Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One-To-Many relation unexpected behavior when not defining foreign key #13884

Closed
hebagomaah opened this issue Nov 5, 2018 · 1 comment
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@hebagomaah
Copy link

hebagomaah commented Nov 5, 2018

I am trying to define a one-to-many relationship as follows:

public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>().HasOne(b => b.Blog).WithMany().IsRequired().OnDelete(DeleteBehavior.Cascade);
            base.OnModelCreating(modelBuilder);
        }
    }

    [Table("Blogs")]
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }

    [Table("Posts")]
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public Blog Blog { get; set; }
    }

The EF defined the relation as required by adding a new column BlogId as a foreign key

but when trying to create the Post below:

{
  "postId": 0,
  "title": "Post1",
  "content": "First post",
  "blog": {
    "blogId": 1
   }
}

and I have previously created a Blog with Id = 1, the exception below is thrown

SqlException: Cannot insert explicit value for identity column in table 'Blogs' when IDENTITY_INSERT is set to OFF.

which mean that EF tries to create the referenced blog.

@ajcvickers
Copy link
Contributor

@hebagomaah Reading between the lines, I'm guessing that you are doing the equivalent of this:

var blog = new Blog { BlogId = 1};
var post = new Post { Blog = blog };
context.Add(post);
context.SaveChanges();

In this case both Blog and Post are treated as new entity instances that need to be inserted. Instead you can use Attach such that the key being set will indicate that the Blog actually already exists:

var blog = new Blog { BlogId = 1};
var post = new Post { Blog = blog };
context.Attach(post);
context.SaveChanges();

See also #13575, https://docs.microsoft.com/en-us/ef/core/saving/related-data, and https://docs.microsoft.com/en-us/ef/core/saving/disconnected-entities

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Nov 5, 2018
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants