EF AddOrUpdate Seed does not Update Child Entities

后端 未结 1 1567
情话喂你
情话喂你 2021-02-19 01:31

I\'m having some issues Seeding data and I was able to reproduce the issue with a very small application.
Given you have this Seed Method:

    protected ove         


        
相关标签:
1条回答
  • 2021-02-19 02:36

    No command in EF automatically updates child - AddOrUpdate is no difference, it internally check existence of the top level entity (Junk in your case) and insert or update it if it doesn't exist. The insertion of children is a side effect in this case because when AddOrUpdate internally calls Add it adds whole object tree. AddOrUpdate does really much less magic than people expect but even with this small amount of magic it should be used only in migration seeding because it is quite expensive operation (additional roundtrip to database for every entity and a lot of reflection).

    Child must always be updated separately - it may require your own separate queries to database to find correct child and common EF update.

    0 讨论(0)
提交回复
热议问题