Entity Framework CTP5 Code-First: How do I specify the type of the discrimimator column in Table-Per-Hierarchy Mapping?

余生长醉 提交于 2019-12-12 12:23:17

问题


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

... and the TPH mapping:

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

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.

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

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

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)?

(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.)


回答1:


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.



来源:https://stackoverflow.com/questions/5053335/entity-framework-ctp5-code-first-how-do-i-specify-the-type-of-the-discrimimator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!