.net - Entity Framework CTP5 代码优先 : How do I specify the type of the discrimimator column in Table-Per-Hierarchy Mapping?

标签 .net entity-framework code-first entity-framework-ctp5

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/

相关文章:

.net - 使用 FxCop 10 GUI 运行代码分析规则集

c# - 如何使用 Automapper 将 DbGeometry 转换为 String?

c# - Entity Framework 为关联对象创建新的/重复的条目

c# - ASP.NET MVC + EF脚手架实现EntityTypeConfiguration类后可以使用吗?

asp.net-mvc - 使用 sqlserver Express 时 Entity Framework 不创建数据库

c# - oo 问题 - 混合 Controller 逻辑和业务逻辑

c# - 用坏字符替换字符串的坏字符

c# - 在不复制源列表的情况下高效地对 IList<T> 进行排序

c# - Entity Framework 代码首次日期字段创建

sql-server - 使用 SQL 脚本创建 ASP.NET Identity 表