开发者

OnModelCreating setting up cascade delete with HasMany causing problems

Consider the 2 alternative lines:

    modelBuilder.Entity<CommissionStructure>().HasMany(c => c.CommissionUnits).WithOptional().WillCascadeOnDelete(true);

    modelBuilder.Entity<CommissionStructure>().HasM开发者_StackOverflow社区any<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!


HasMany as a method of EntityTypeConfiguration<T> has this signature:

HasMany<TTarget>(Expression<Func<T, ICollection<TTarget>>> expression)

which means that you cannot use IEnumerable<T> for navigation properties. You need ICollection<T> or a derived collection type. This explains both compiler errors you had.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜