本文介绍了错误重映射EF代码第一TPH鉴别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述









当我使用以下任一变体映射时:

  modelBuilder.Entity< MyBase>()
.Map< MyBase>(m => m.Requires(TypeId)。HasValue(0))
.Map< DerivedA>(m => m.Requires(TypeId)。HasValue(1))
.Map< DerivedB>(m => m.Requires peId)。HasValue(2))
.Map< DerivedC>(m => m.Requires(TypeId)。HasValue(3))
.Map< DerivedD>(m => m.Requires(TypeId)。HasValue(4));

  modelBuilder.Entity< MyBase>()
.Map< MyBase>(m => m.Requires(TypeId)。HasValue(0));
modelBuilder.Entity< MyBase>()
.Map< DerivedA>(m => m.Requires(TypeId)。HasValue(1));
modelBuilder.Entity< MyBase>()
.Map< DerivedB>(m => m.Requires(TypeId)。HasValue(2));
modelBuilder.Entity< MyBase>()
.Map< DerivedC>(m => m.Requires(TypeId)。HasValue(3));
modelBuilder.Entity< MyBase>()
.Map< DerivedD>(m => m.Requires(TypeId)。HasValue(4));

具有以下变体:




  • 使用字符串而不是整数,例如1

  • 删除MyBase的Map语句



我收到错误:

所有派生类都直接从MyBase继承,所有派生类都包含在映射中。



我正在使用Entity Framework 4.3.1。



我做错了什么?

解决方案

这是在EF5-beta2中修复的,其中链接的地图通话应该可以工作。



在EF 4.3中,您将需要拆分将每个Map调用到其应用的实体类型的EntityTypeConfiguration

  modelBuilder.Entity< ; MyBase>()
.Map< MyBase>(m => m.Requires(TypeId)。HasValue(0));
modelBuilder.Entity< DerivedA>()
.Map< DerivedA>(m => m.Requires(TypeId)。HasValue(1));
modelBuilder.Entity< DerivedB>()
.Map< DerivedB>(m => m.Requires(TypeId)。HasValue(2));
modelBuilder.Entity< DerivedC>()
.Map< DerivedC>(m => m.Requires(TypeId)。HasValue(3));
modelBuilder.Entity< DerivedD>()
.Map< DerivedD>(m => m.Requires(TypeId)。HasValue(4));注意调用实体< DerivedA> 实体< DerivedB> 等等,而不是全部到实体< MyBase>


I'm attempting to remap the descriminator column of my TPH-persisted object hierarchy as described at:

http://msdn.microsoft.com/en-us/library/hh295845(v=vs.103).aspx

http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

https://stackoverflow.com/a/6650064/141172

When I map using either of the following variants:

modelBuilder.Entity<MyBase>()
            .Map<MyBase>(m => m.Requires("TypeId").HasValue(0))
            .Map<DerivedA>(m => m.Requires("TypeId").HasValue(1))
            .Map<DerivedB>(m => m.Requires("TypeId").HasValue(2))
            .Map<DerivedC>(m => m.Requires("TypeId").HasValue(3))
            .Map<DerivedD>(m => m.Requires("TypeId").HasValue(4));

or

modelBuilder.Entity<MyBase>()
            .Map<MyBase>(m => m.Requires("TypeId").HasValue(0));
modelBuilder.Entity<MyBase>()
            .Map<DerivedA>(m => m.Requires("TypeId").HasValue(1));
modelBuilder.Entity<MyBase>()
            .Map<DerivedB>(m => m.Requires("TypeId").HasValue(2));
modelBuilder.Entity<MyBase>()
            .Map<DerivedC>(m => m.Requires("TypeId").HasValue(3));
modelBuilder.Entity<MyBase>()
            .Map<DerivedD>(m => m.Requires("TypeId").HasValue(4));

With the following variations:

  • Use string rather than integer, e.g. "1"
  • Remove Map statement for MyBase

I get the error:

All derived classes inherit directly from MyBase, and all derived classes are included in the mapping.

I'm using Entity Framework 4.3.1.

What am I doing wrong?

解决方案

This is fixed in EF5-beta2 where the chained Map calls should work.

In EF 4.3 you will need to split out each Map call onto an EntityTypeConfiguration for the entity type to which it applies.

modelBuilder.Entity<MyBase>()
        .Map<MyBase>(m => m.Requires("TypeId").HasValue(0));
modelBuilder.Entity<DerivedA>()
        .Map<DerivedA>(m => m.Requires("TypeId").HasValue(1));
modelBuilder.Entity<DerivedB>()
        .Map<DerivedB>(m => m.Requires("TypeId").HasValue(2));
modelBuilder.Entity<DerivedC>()
        .Map<DerivedC>(m => m.Requires("TypeId").HasValue(3));
modelBuilder.Entity<DerivedD>()
        .Map<DerivedD>(m => m.Requires("TypeId").HasValue(4));

Notice the calls to Entity<DerivedA>, Entity<DerivedB>, and so on rather than all to Entity<MyBase>.

这篇关于错误重映射EF代码第一TPH鉴别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:51