gpt4 book ai didi

entity-framework - Entity Framework 4.3 到 Entity Framework 5 映射异常

转载 作者:行者123 更新时间:2023-12-01 01:16:02 33 4
gpt4 key购买 nike

我正在从 迁移项目 Entity Framework 4.3 在 .net 4 上运行到 Entity Framework 5 在 .net 4.5 上运行。在不做任何更改的情况下,当我尝试运行该项目时,代码优先模型配置失败并显示 System.Data.MappingException 消息:

(495,10) : error 3034: Problem in mapping fragments starting at lines 495, 536:Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows.

[5 other similar paragraphs removed]


该消息未指定导致问题的实体或关系,而且我的模型相当复杂。有什么方法可以让我获得一些更有用的信息,以便更轻松地诊断问题?

最佳答案

拉迪斯拉夫提出的继承问题是正确的。当涉及到代码优先的 Table Per Hierarchy 配置时,Entity Framework 4.3 和 Entity Framework 5 的行为似乎有些不同。

在这种情况下,我有四个派生类型,每个类型都有自己的从 EntityTypeConfiguration<T> 派生的配置类。 .基本抽象类型没有向模型构建器注册的配置。这在 EF 4.3 下不是问题,它只是创建了一个以基本类型命名的表,并带有一个“鉴别器”列来区分类型。

要获得与 EF 5 相同的行为,必须创建一个空的配置类

public class MyBaseConfiguration : EntityTypeConfiguration<MyBase> 
{
// Nothing happening here
}

然后在模型构建器中注册
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MyBaseConfiguration());

// Add configurations for derived and other types as normal
}
}

关于entity-framework - Entity Framework 4.3 到 Entity Framework 5 映射异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035174/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com