What triggers Entity Framework to fix up a navigation property?

前端 未结 2 879
既然无缘
既然无缘 2020-12-06 03:37

I couldn\'t find good documentation on just exactly what makes Entity Framework decide to look up the correct related object when the foreign key is set.

I\'m using

2条回答
  •  醉酒成梦
    2020-12-06 04:01

    When using change tracking entities, one of the pair of navigation properties is fixed up when the other navigation is set. For example, say there is a principal entity navigation from Post to Blog and there is a dependent collection property from Blog to Post. If you are using change tracking entities then setting post.Blog = blog will also result in blog.Posts.Add( post). This can be a bit confusing if you are used to doing both post.Blog = blog and then blog.Posts.Add( post) in your code prior to SaveChanges. When you are using DetectChanges this is harmless, but when using change tracking entities you will get two copies of the post in the blog.Posts collection, prior to SaveChanges.

提交回复
热议问题