Entity Framework CTP5 Code-First: how to specify the type of discriminator column in a table mapping for a hierarchy?

This ADO.NET team blog post shows in an example how to define table mapping by hierarchy in the Fluent API of the Framework Code Object First. This is a (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 TPH mapping:

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

Type here is clearly a β€œclean” discriminator column in the database table that does not have a related property in the model classes. This is wonderful and what I want.

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

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

For example: I have a Type discriminator column with possible values ​​of "A", "B" or "C" to distinguish between my derived model classes. How to configure a SQL Server column to be of type varchar(1) ?

(I tried using just the character value by setting m => m.Requires("Type").HasValue('A') for example m => m.Requires("Type").HasValue('A') (note the single quotes). But this throws an exception telling me that the char type is not valid as discriminator column.)

+4
source share
1 answer

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

However, I showed how to change the column type to int in this article , but as they confirm it is not fully supported for all types with CTP5.

+3
source

Source: https://habr.com/ru/post/1340481/


All Articles