开发者

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

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 wh开发者_高级运维ich 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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜