问题描述
我正在尝试做这样的事情:
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; }
}
但我猜不支持 DbSet>
.我收到此错误:
But I guess DbSet<AuditLog<string>>
is not supported. I get this error:
附加信息:未映射类型TrackerEnabledDbContext.AuditLog`1[System.String]".使用 Ignore 方法或 NotMappedAttribute 数据注释检查是否未明确排除该类型.验证类型是否定义为类,不是原始类型或泛型,并且不继承自 EntityObject.
有什么方法可以实现 public DbSet>审计日志{获取;放;}
?
Is there any ways I can implement public DbSet<AuditLog<string>> AuditLog { get; set; }
?
推荐答案
您无法映射泛型类型,因为 Entity Framework 根本不支持泛型实体类型.使用 EF Code-First 方法时,您需要记住应该对 POCO 类 允许实体框架创建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:
- 不应包含任何属性
- 不应该是通用的
- 应该是公开的
- 不得密封
- 不能是抽象的
- 必须有一个没有参数的公共或受保护的构造函数
这篇关于泛型类是否不支持作为实体框架中的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!