且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架复合键,级联删除

更新时间:2022-11-05 08:34:00

我把这个答案放在这里,可能会碰到与我一样的问题。我需要将所有的FK放入PK FK(因为它们不允许为空)。这有点烦人,因为如果你有一个更复杂的树,你必须管理的密钥数量会越来越深。

Figured out what I was missing. I'm putting this answer here for anyone that might bump into the same problem as I have. I needed to make all FK into PK FK (since they don't allow null). It's a bit annoying since if you have an even more complex tree, the number of keys you'd have to be manage of would grow the deeper you go.

modelBuilder.Entity<Table3>().HasKey(m => new {m.Id, m.Table2Id, m.Table1Id});

如果有人想知道如何缩短要管理的密钥数量,请留下答案。因为这可能不是***的解决方案。

If anyone has an idea on how to shorten the number of keys to manage please leave an answer. Since this might not be the best solution.