C# EF代码中的泛型类优先

C# EF代码中的泛型类优先,c#,entity-framework,generics,ef-code-first,C#,Entity Framework,Generics,Ef Code First,我正在尝试在我的应用程序中加入EF。我首先使用EF6和代码。我的对象模型有一些泛型类,代码似乎首先接受它,部分表和列是正确生成的 下面是一个例子: public class Group : TimeableObject<GroupTimeline> {} public class GroupTimeline : TimelineBase { public GroupTimeline() { } public string Name {get; set;} } pu

我正在尝试在我的应用程序中加入EF。我首先使用EF6和代码。我的对象模型有一些泛型类,代码似乎首先接受它,部分表和列是正确生成的

下面是一个例子:

public class Group : TimeableObject<GroupTimeline> {}

public class GroupTimeline : TimelineBase
{
    public GroupTimeline() { }
    public string Name {get; set;}
}

public abstract class TimelineBase
{
    public int Id { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime EndDate { get; set; }
}

public abstract class TimeableObject<T>
    where T : TimelineBase
{
    private DateTime? _snapshotDate;

    public TimeableObject()
    {
        Timelines = new List<T>();
    }

    public int Id { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime EndDate { get; set; }

    public List<T> Timelines { get; set; }

    public DateTime SnapshotDate
    {
        get { //some logic.. }
        set { _snapshotDate = value; }
    }

    public T Timeline
    {
        get { //some logic.. }
    }
}
并且我可以防止映射SnapshotDate属性:

[NotMapped]
下面是我的流畅配置:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Group>().Map(m =>
        {
            m.MapInheritedProperties();
        });
        modelBuilder.Entity<GroupTimeline>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Group_Timeline");
        });

      modelBuilder.Entity<TimeableObject<TimelineBase>>().HasKey(m => m.Id).Property( p => p.Id ).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      modelBuilder.Entity<TimeableObject<TimelineBase>>().Ignore(m => m.SnapshotDate);
      modelBuilder.Entity<TimelineBase>().HasKey(m => m.Id).Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
问候,,
Daniel Kaya

要使用fluent API忽略属性,请看一看这个问题:我尝试使用ignore API。我刚刚编辑了帖子并添加了配置。这个答案表明EF不支持泛型类型作为模型:我不同意这个信息。EF确实支持泛型。通过使用数据注释,可以像我的文章中的例子一样。因此,结论应该是,在某些情况下,使用fluentapi可以获得更多的数据注释。
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Group>().Map(m =>
        {
            m.MapInheritedProperties();
        });
        modelBuilder.Entity<GroupTimeline>().Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Group_Timeline");
        });

      modelBuilder.Entity<TimeableObject<TimelineBase>>().HasKey(m => m.Id).Property( p => p.Id ).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      modelBuilder.Entity<TimeableObject<TimelineBase>>().Ignore(m => m.SnapshotDate);
      modelBuilder.Entity<TimelineBase>().HasKey(m => m.Id).Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
The type 'Data.DataObjectModel.TimeableObject`1[Data.DataObjectModel.TimelineBase]' 
was not mapped. Check that the type has not been explicitly excluded by using the 
Ignore method or NotMappedAttribute data annotation. Verify that the type was 
defined as a class, is not primitive or generic, and does not inherit from 
EntityObject.