This blog post ADO.NET 团队的一个例子展示了如何在 Entity Framework Code-First 的 Fluent API 中定义 Table-Per-Hierarchy 映射。这是(稍微简化的)示例:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
// more properties
}
public class DiscontinuedProduct : Product
{
public DateTime DiscontinuedDate { get; set; }
}
...和TPH映射:
modelBuilder.Entity<Product>()
.Map<Product>(m => m.Requires("Type").HasValue("Current"))
.Map<DiscontinuedProduct>(m => m.Requires("Type").HasValue("Old"));
Type
这里显然是数据库表中的“纯”鉴别器列,它在模型类中没有相关属性。那很好,也是我想要的。如果我让为这个映射创建数据库表,列
Type
在 SQL Server 中有类型 nvarchar(MAX)
.有没有办法在 Fluent API 中配置鉴别器列的类型?
例如:我有一个鉴别器列
Type
可能的值 "A", "B" or "C"
区分我的派生模型类。如何配置 SQL Server 中的列的类型为 varchar(1)
?(我试图通过设置例如
m => m.Requires("Type").HasValue('A')
(注意单引号)来简单地使用字符值。但这会引发一个异常,告诉我 char
类型不允许作为鉴别器列。)
最佳答案
英孚团队 confirms here当前不支持更改 TPH 中鉴别器列的类型。这是他们正在研究的东西,看看他们是否可以在 RTM 之前启用它。
也就是说,我展示了如何在 this article 中将列类型更改为 int但就像他们确认的那样,从 CTP5 开始,所有类型都无法完全支持这一点。
关于.net - Entity Framework CTP5 代码优先 : How do I specify the type of the discrimimator column in Table-Per-Hierarchy Mapping?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5053335/