Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Integer Discriminator Column

According to this source

http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

it should be possible to have the TPH discriminator column be an integer:

Also, changing the data type of discriminator column is interesting. In the above code, we passed strings to HasValue method but this method has been defined to accepts a type of object:

public void HasValue(object value);

Therefore, if for example we pass a value of type int to it then Code First not only use our desired values (i.e. 1 & 2) in the discriminator column but also changes the column type to be (INT, NOT NULL):

modelBuilder.Entity() .Map(m => m.Requires("BillingDetailType").HasValue(1)) .Map(m => m.Requires("BillingDetailType").HasValue(2));

However, when I do that in my code I see discriminator values like "1" and "2", but the column type is still

nvarchar(128), not null

Is it in fact possible to specify an integer discriminator column? If so, how?

I'm certain that I specify my mapping as .HasValue(1) and not .HasValue("1").

like image 533
Eric J. Avatar asked Apr 28 '26 15:04

Eric J.


1 Answers

modelBuilder.Entity<X>()
    .Map<X>(m => { m.Requires("BillingDetailType").HasValue(0).HasColumnType("tinyint"); })
    .Map<Y>(m => { m.Requires("BillingDetailType").HasValue(1); m.MapInheritedProperties(); })
    .Map<Z>(m => { m.Requires("BillingDetailType").HasValue(2); m.MapInheritedProperties(); })
    ;
like image 98
user2170796 Avatar answered May 01 '26 09:05

user2170796



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!