实体框架CTP5代码优先:如何在“按表分层结构映射"中指定discrimimator列的类型? [英] Entity Framework CTP5 Code-First: How do I specify the type of the discrimimator column in Table-Per-Hierarchy Mapping?

查看:126
本文介绍了实体框架CTP5代码优先:如何在“按表分层结构映射"中指定discrimimator列的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ADO.NET团队的博客文章在一个示例中显示了如何在Entity Framework Code-First的Fluent API中定义每层表映射.这是(略有简化的)示例:

This blog post of the ADO.NET team shows in an example how to define Table-Per-Hierarchy Mapping in the Fluent API of Entity Framework Code-First. This is the (slightly simplified) example:

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映射:

... and the TPH mapping:

modelBuilder.Entity<Product>()
    .Map<Product>(m => m.Requires("Type").HasValue("Current"))
    .Map<DiscontinuedProduct>(m => m.Requires("Type").HasValue("Old")); 

Type在这里显然是数据库表中的纯"区分符列,在模型类中没有任何相关属性.很好,就是我想要的.

Type is here obviously a "pure" discriminator column in the database table which has no related property in the model classes. That's fine and what I want.

如果让我为此映射创建数据库表,则列Type在SQL Server中的类型为nvarchar(MAX).

If I let create the DB tables for this mapping, the column Type has in SQL Server the type nvarchar(MAX).

是否可以在Fluent API中配置鉴别符列的类型?

Is there a way to configure the type of the discriminator column in the Fluent API?

例如:我有一个鉴别符列Type和可能的值"A", "B" or "C"来区分我的派生模型类.如何配置SQL Server中的列的类型为varchar(1)?

For instance: I have a discriminator column Type with possible values "A", "B" or "C" to distinguish between my derived model classes. How can I configure that the column in SQL Server has type varchar(1)?

(我试图通过设置实例m => m.Requires("Type").HasValue('A')(注意单引号)来简单地使用字符值.但是这引发了一个异常,告诉我不允许将char类型用作鉴别符列.)

(I have tried to use simply a character value by setting for instance m => m.Requires("Type").HasValue('A') (note the single quotes). But this throws an exception telling me that a char type is not allowed as discriminator column.)

推荐答案

EF团队

The EF team confirms here that change the type of the discriminator column in TPH is not currently supported. This is something that they are looking into to see if they can enable it before the RTM.

也就是说,我在

That said, I showed how to change the column type to int in this article but like they confirm this is not fully supported for all types as of CTP5.

这篇关于实体框架CTP5代码优先:如何在“按表分层结构映射"中指定discrimimator列的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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