OnModelCreating 设置级联删除与 HasMany 导致问题
考虑 2 个替代行:
modelBuilder.Entity<CommissionStructure>().HasMany(c => c.CommissionUnits).WithOptional().WillCascadeOnDelete(true);
modelBuilder.Entity<CommissionStructure>().HasMany<CommissionUnit>(c=>c.CommissionUnits).WithOptional().WillCascadeOnDelete(true);
第一行抱怨无法从用法中推断 TTarget。
所以我尝试了第二行,但是它现在抱怨无法将 IEnumerable 转换为 ICollection,即使我的模型没有在任何地方定义为 ICollection。
有什么想法吗? CommissionUnits 是 CommissionStructure 内的嵌套 IEnumerable 集合。这之前是一个 IList,但是当在我的存储库中使用 OrderBy 的 LINQ 查询执行 ToList() 时,IList 会出现问题。这就是我在存储库中公开 IEnumerable 而不是 IList 的原因。所以我有点进退两难。一个艰难的地方!
Consider the 2 alternative lines:
modelBuilder.Entity<CommissionStructure>().HasMany(c => c.CommissionUnits).WithOptional().WillCascadeOnDelete(true);
modelBuilder.Entity<CommissionStructure>().HasMany<CommissionUnit>(c=>c.CommissionUnits).WithOptional().WillCascadeOnDelete(true);
First line complains about TTarget cannot being inferred from the usage.
So I tried the 2nd line, however it now complains about not being able to convert IEnumerable to ICollection even though my model is not defined anywhere as an ICollection.
Any ideas? The CommissionUnits are a nested IEnumerable collection inside CommissionStructure. This was an IList before, but IList has problems when doing a ToList() from a LINQ query in my repository where OrderBy has been used. This is why I exposed IEnumerable not IList on my repository. So I'm kind of stuck between a rock & a hard place!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HasMany
作为EntityTypeConfiguration
的方法具有以下签名:这意味着您不能将
IEnumerable
用于导航属性。您需要ICollection
或派生集合类型。这解释了您遇到的两个编译器错误。HasMany
as a method ofEntityTypeConfiguration<T>
has this signature:which means that you cannot use
IEnumerable<T>
for navigation properties. You needICollection<T>
or a derived collection type. This explains both compiler errors you had.