【发布时间】:2012-03-25 20:59:13
【问题描述】:
我有以下型号。
interface IKeywordedEntity
{
IEntityCollection<Keyword> Keywords { get; }
}
class Foo : EntityBase, IKeywordedEntity
{
public virtual IEntityCollection<Keyword> Keywords { get { ... } }
}
class Bar : EntityBase, IKeywordedEntity
{
public virtual IEntityCollection<Keyword> Keywords { get { ... } }
}
我想编写一个扩展方法来自动处理OnModelCreating 中的每个关键字。
public static void WithKeywords<TEntityType>(this EntityTypeConfiguration<TEntityType>
entityTypeConfiguration) where TEntityType : EntityBase, IKeywordedEntity
{
entityTypeConfiguration.HasMany(e => e.Keywords).WithMany();
}
所以我在OnModelCreating 中这样调用它。
modelBuilder.Entity<Foo>.WithKeywords();
modelBuilder.Entity<Bar>.WithKeywords();
但是,我得到以下异常:
导航属性“关键字”不是类型上的声明属性 '福'。验证它没有被明确地从模型中排除 并且它是一个有效的导航属性。
我该怎么做才能让这个扩展方法起作用?
【问题讨论】:
标签: c# entity-framework ef-code-first