Cannot rename Discriminator column in Entity Framework 4.1 Code First database

前提是你 提交于 2019-12-23 16:26:14

问题


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?


回答1:


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.




回答2:


You cannot map discriminator for C because it is not part of your TPH hierarchy. Try just this:

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


来源:https://stackoverflow.com/questions/6638626/cannot-rename-discriminator-column-in-entity-framework-4-1-code-first-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!