简体   繁体   English

实体框架中是否不支持通用类作为模型?

[英]Are generic classes not supported as models in Entity Framework?

I am trying to do something like this : 我想做这样的事情:

public class TrackerContext : DbContext
{
    public bool TrackNewValues { get; set; }

    public TrackerContext(bool trackNewValues = false)
        : base()
    {
        TrackNewValues = trackNewValues;
    }

    public TrackerContext(string connectinString, bool trackNewValues = false)
        : base(connectinString)
    {
        TrackNewValues = trackNewValues;
    }

    public DbSet<AuditLog<string>> AuditLog { get; set; }
    public DbSet<AuditLogChild> LogChildren { get; set; }
}



public class AuditLog<UserIdOrUserNameColumnType>
{
    public AuditLog()
    {
        Children = new List<AuditLogChild>();
    }

    [Key]
    public Guid AuditLogID { get; set; }

    public UserIdOrUserNameColumnType UserId { get; set; }

    [Required]
    public DateTimeOffset EventDateUTC { get; set; }
}

But I guess DbSet<AuditLog<string>> is not supported. 但我猜不支持DbSet<AuditLog<string>> I get this error: 我收到此错误:

Additional information: The type 'TrackerEnabledDbContext.AuditLog`1[System.String]' was not mapped. 附加信息:未映射类型'TrackerEnabledDbContext.AuditLog`1 [System.String]'。 Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. 使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型。 Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject. 验证类型是否已定义为类,不是原始类型还是通用类型,并且不从EntityObject继承。

Is there any ways I can implement public DbSet<AuditLog<string>> AuditLog { get; set; } 有什么方法可以实现public DbSet<AuditLog<string>> AuditLog { get; set; } public DbSet<AuditLog<string>> AuditLog { get; set; } public DbSet<AuditLog<string>> AuditLog { get; set; } ? public DbSet<AuditLog<string>> AuditLog { get; set; }

You cannot map the generic type because Entity Framework simply doesn't support generic Entity types. 您无法映射泛型类型,因为实体框架根本不支持通用实体类型。 When using the EF Code-First approach you need to remember that you should model your POCO classes within the constraints that allow Entity Framework to create POCO proxies . 使用EF Code-First方法时,您需要记住,您应该在允许Entity Framework创建POCO代理的约束内对POCO类进行建模。

This means, shortly speaking that such a class: 这意味着,很快就会说出这样一个类:

  • Should not contain any attributes 不应包含任何属性
  • Should not be generic 不应该是通用的
  • Should be public 应该是公开的
  • Must not be sealed 不得密封
  • Must not be abstract 一定不是抽象的
  • Must have a public or protected constructor that does not have parameters 必须具有没有参数的公共或受保护构造函数

I have been using generic classes with success in Entity Framework. 我一直在Entity Framework中成功使用泛型类。 If you declare your class and DbSet the following way it will work. 如果以下列方式声明您的类和DbSet,它将起作用。

public class AuditLogString : AuditLog<String>{}

public DbSet<AuditLogString>  AuditLogStrings { get;set;}

[Update] I have not used this method recently and in the light of the comments on this answer I suggest Pawel's answer instead. [更新]我最近没有使用过这种方法,根据对这个答案的评论,我建议Pawel的回答。 However I have not deleted this answer since I was able to use the method. 但是我没有删除这个答案,因为我能够使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM