Entity Framework Code First - two Foreign Keys from same table

后端 未结 6 1929
别那么骄傲
别那么骄傲 2020-11-22 03:40

I\'ve just started using EF code first, so I\'m a total beginner in this topic.

I wanted to create relations between Teams and Matches:

1 match = 2 teams (ho

6条回答
  •  Happy的楠姐
    2020-11-22 03:58

    This is because Cascade Deletes are enabled by default. The problem is that when you call a delete on the entity, it will delete each of the f-key referenced entities as well. You should not make 'required' values nullable to fix this problem. A better option would be to remove EF Code First's Cascade delete convention:

    modelBuilder.Conventions.Remove(); 
    

    It's probably safer to explicitly indicate when to do a cascade delete for each of the children when mapping/config. the entity.

提交回复
热议问题