如何在Entity Framework 4.3中执行多对多关系?

时间:2012-03-26 12:11:18

标签: entity-framework entity-framework-4.1 many-to-many

我有以下课程:

public class Entity
{
    public long Id {get;set;}
}

public abstract class Base
{
    public long Id {get;set;}
    public abstract ICollection<Entity> Entities {get;set;}
}

public class Child : Base
{
    public override ICollection<Entity> Entities {get;set;}
}

映射:

modelBuilder.Entity<Child>().Map(
    m => {
            m.ToTable("Children");
            m.MapInheritedProperties();
         });
modelBuilder.Entity<Child>().HasMany(m => m.Entities).WithMany();

我得到以下异常:

The navigation property 'Entities' is not a declared property on type
'EventCriteria'. Verify that it has not been explicitly excluded from the model
and that it is a valid navigation property.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在映射TPC继承(您调用MapInheritedProperties),在这种情况下,Entities集合必须映射到Base实体,而不是Child实体。