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; }
... 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.)
source share