Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot rename Discriminator column in Entity Framework 4.1 Code First database

I have a model hierarchy configuration like so:

[Table("A")]
abstract class A { }

class B : A { } // treated as abstract

[Table("C")]
class C : B { }

This results in a TPH for A and B, and a TPT for C. So far this seems to work fine. There are two database tables being generated: "A" which contains both A and B model records, and "C" which contains only C model record columns not already kept in table "A".

For the TPH arrangement on Table A, there is a generated "Discriminator" column, which EF CF creates on its own to distinguish type A vs type B. This column is fine and expected; however, I would like to rename it. For this post's sake, the new name might be "Type".

The tutorials that document how to do this appear to suggest this:

modelBuilder.Entity<A>()
   .Map<B>(m=>m.Requires("Type").HasValue(typeof(B).Name))
   .Map<C>(m=>m.Requires("Type").HasValue(typeof(C).Name));

This does not seem to work, though, as I get a runtime error during database generation saying that the models of types A, B, and C are being added to the same table, and, "Mapping conditions can be used to distinguish the rows that these types are mapped to." (Whatever this means.)

I also tried:

modelBuilder.Entity<A>()
    .Map<B>(m=>m.Requires("Type"))
    .Map<C>(m=>m.Requires("Type"));

.. as well as ..

modelBuilder.Entity<A>().Map(m=>m.Requires("Type"));

.. and even though these attempts compile and produce no runtime errors, there seems to be no effect, as the Discriminator column remains as "Discriminator".

I tried to create a new string property on A called "Discriminator", to which I was going to subsequently rename the property's column metadata, but that just gave me two "Discriminator" columns: "Discriminator" and "Discriminator1".

Ideas?

like image 613
Jon Davis Avatar asked Jul 20 '26 20:07

Jon Davis


1 Answers

Not strictly an answer but just confirmation that the approach should work...

I have just managed to rename the discriminator field in a TPH structure we're using. The hierarchy has an enum value to distinguish types.
A is the base class, B is a slightly more refined version, C,D and E are the concrete classes.

class A
class B : A
class C : B
class D : B
class E : A

modelBuilder.Entity<A>()
            .Map<B>(m => m.Requires("TypeId").HasValue((int)MyType.ClassB))
            .Map<C>(m => m.Requires("TypeId").HasValue((int)MyType.ClassC))
            .Map<D>(m => m.Requires("TypeId").HasValue((int)MyType.ClassD))
            .Map<E>(m => m.Requires("TypeId").HasValue((int)MyType.ClassE));

I had to remember to include every derived class type even if it was never used as a concrete class. Originally I forgot to include B as I was thinking it was not required and the discriminator field would keep appearing.

I can imagine EF getting a little confused if you try to map your C class even though it is in a different table.

The error message you mention in your comment sounds like some of the HasValue requirements are producing the same value for different class types - perhaps.

Edit:
Just read this which seems to deal with the same error message.

like image 186
shunty Avatar answered Jul 22 '26 10:07

shunty



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!