是通用类不支持作为实体框架模型? [英] Are generic classes not supported as models in Entity Framework?

查看:234
本文介绍了是通用类不支持作为实体框架模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情:

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; }
}

不过,我想 DbSet&LT;审计日志&LT;串GT;&GT; 不支持。我得到这个错误:

But I guess DbSet<AuditLog<string>> is not supported. I get this error:

附加信息:类型'TrackerEnabledDbContext.AuditLog`1 [System.String]'没有映射。
      检查类型尚未明确使用方法忽略或NotMappedAttribute数据注解排除。
      验证被定义为一类的类型,不是原始或通用的,并且不从EntityObject继承

Additional information: The type 'TrackerEnabledDbContext.AuditLog`1[System.String]' 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.

有没有我可以实现任何方式公共DbSet&LT;审计日志&LT;串GT;&GT;审计日志{搞定;组; }

Is there any ways I can implement public DbSet<AuditLog<string>> AuditLog { get; set; } ?

推荐答案

由于实体框架根本不支持泛型类型的实体不能映射泛型类型。
当使用EF code-First方法,你需要记住,你应该型号你的 POCO类,使实体框架创建的的=http://msdn.microsoft.com/en-us/library/vstudio/dd468057 %28V = VS.100%29.aspx相对=nofollow> POCO代理

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.

这意味着,短期内来讲,这样的类:

This means, shortly speaking that such a class:


  • 如果不包含任何属性

  • 不应该是通用

  • 应该是公开的

  • 不得被密封

  • 绝不能是抽象

  • 必须有一个没有参数公共或受保护的构造

这篇关于是通用类不支持作为实体框架模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆