无法重命名Entity Framework 4.1 Code First数据库中的Discriminator列 [英] Cannot rename Discriminator column in Entity Framework 4.1 Code First database

查看:448
本文介绍了无法重命名Entity Framework 4.1 Code First数据库中的Discriminator列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型层次结构,如下所示:

  [Table(A)] 
抽象类A {}

class B:A {} //处理为抽象

[表(C)]
类C:B {}

这导致A和B的TPH,以及C的TPT。到目前为止,这似乎工作正常。生成两个数据库表:A,其中包含A和B模型记录,C仅包含尚未保存在表A中的C模型记录列。



对于表A中的TPH安排,有一个生成的Discriminator列,EF CF自行创建以区分A类与B类。此列是罚款和预期的;但是,我想重命名它。对于这个帖子,新​​的名字可能是Type。



介绍如何执行此操作的教程似乎提示:

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

尽管如此,在数据库生成过程中出现运行时错误时,似乎不起作用,类型A,B和C的模型正被添加到同一个表中,并且映射条件可用于区分这些类型映射到的行。 (无论这是什么意思)。



我也尝试过:

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

..以及..

  modelBuilder.Entity< a取代;()地图(M => m.Requires( 类型)); 

..即使这些尝试编译并产生运行时错误,似乎没有任何效果,因为Discriminator列保持为Discriminator。



我尝试在A上创建一个名为Discriminator的新字符串属性,稍后我将重命名该属性的列元数据,但这只是给了我两个Discriminator列:Discriminator和Discriminator1。



想法?

解决方案

不是严格的答案,只是确认方法应该工作...



我刚刚设法重命名我们正在使用的TPH结构中的鉴别器字段。
层次结构具有区分类型的枚举值。

A是基类,B是稍微更精细的版本,C,D和E是具体的类。



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 ).HasValue((int)MyType.ClassC))
.Map&D;(m => m.Requires(TypeId)。HasValue((int)MyType.ClassD))
.Map< E>(m => m.Requires(TypeId)。HasValue((int)MyType.ClassE));

我不得不记得,包含每个派生类类型,即使是从来没有用过具体的课。原来我忘了包含B,因为我认为这不是必需的,并且鉴别器字段将继续出现。



如果您尝试映射,我可以想像EF有点困惑您的C类,即使它在不同的表格。



您在评论中提到的错误消息听起来像一些HasValue要求正在为不同的类生成相同的值类型 - 也许。



编辑:

只读这个似乎处理相同的错误消息。 p>

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?

解决方案

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.

这篇关于无法重命名Entity Framework 4.1 Code First数据库中的Discriminator列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆