gpt4 book ai didi

c# - EntitySet System.InvalidOperationException - "the entity type is not part of the model for the current context"

转载 作者:太空狗 更新时间:2023-10-29 21:45:40 25 4
gpt4 key购买 nike

类似问题

The entity type <classname> is not part of the model for the current context -和- EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context是类似的问题,但它们只是“代码优先”的观点,具有更简单的数据模型,并解决连接字符串和映射问题。请仔细看这个。

症状

// HomeController.cs
public ActionResult Index()
{
var _db = new MealsContext();

var m = _db.Meals.ToList();
var d = _db.Drinks.ToList();

return View();
}

检索 Drinks 集合时抛出异常:

The entity type Drink is not part of the model for the current context.

代码

// Meal.cs
public class Meal
{
public int Id { get; set; }
public string Stuff { get; set; }
public virtual ICollection<Meat> Meats { get; set; }
public virtual ICollection<Vegetable> Vegetables { get; set; }
}

// Meat.cs
public class Meat
{
public int Id { get; set; }
public string Name { get; set; }
public int MealId { get; set; }
}

// Vegetable.cs
public class Vegetable
{
public int Id { get; set; }
public string Name { get; set; }
public int MealId { get; set; }
}

// Drink.cs
public class Drink
{
public int Id { get; set; }
public string Name { get; set; }
}

是的,我知道在现实世界中 Meat 和 Vegetable 与 Meals 之间的关系可能是多对多关系,但不要在这里挂断。

// MealsContext.cs
public class MealsContext: DbContext
{
public MealsContext() : base("ConnectionString")

public DbSet<Meal> Meals{ get; set; }
public DbSet<Meat> Meats{ get; set; }
public DbSet<Vegetable> Vegetables { get; set; }
public DbSet<Drink> Drinks{ get; set; }
}

我的经验是使用模型优先方法。 EDMX 文件是在 POCO 之后构建的。

在连接字符串中是映射到已解析 EDMX 资源的元数据部分 (metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl;).

我检查了 EDMX 文件的底层 XML,显示了概念模型和商店模型中存在的所有实体,并且所有实体都已完全映射。什么鬼?

疑难解答

第一个尝试是完全摆脱存储和映射 EDMX 数据(SSDLMSL 部分)。开火,现在有两个异常(exception):

  1. 检索 Meals 抛出 MSL,错误 2062 没有为 EntityContainer 中的 EntitySet 和 AssociationSet 实例指定映射

  2. 检索 Drinks 继续抛出 The entity type Drinkis not part of the model for the current context

Meals 抛出的错误是预料之中的,我修改了映射和存储模型——检查 _db 显示 Meals -> InternalSet -> EntitySet 属性是正确的,只是没有映射。

Drinks 抛出的错误是我卡住的地方。仔细检查 _db 显示 Drinks -> InternalSet -> EntitySet 抛出 SystemInvalidOperation 声明实体不在模型上下文中的异常。

这是 EDMX 的 CSDL 的 XML 格式:

<edmx:ConceptualModels>
<Schema ...>
<EntityContainer Name="MealsContext" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Meals" EntityType="Models.Meal" />
<EntitySet Name="Meats" EntityType="Models.Meat" />
<EntitySet Name="Vegetables" EntityType="Models.Vegetable" />
<EntitySet Name="Drinks" EntityType="Models.Drink" />
<!-- AssociationSets here for the FKs -->
</EntityContainer>
<!-- All are present, but here's the culprit Drink -->
<EntityType Name="Drink">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" MaxLength="200" FixedLength="false" Unicode="true" />
</EntityType>
<!-- Associations here -->
</Schema>
</edmx:ConceptualModels>

问题

如果 DbContext 具有所有 DbSet 属性并且正在使用一个连接字符串,该连接字符串包含一个模型的元数据,该模型的 CSDL 正确定义了实体类型 Drink为什么它不是上下文的一部分?

我能看到的 Drink 唯一不同的是它与任何其他实体都不相关,也没有任何关联...

最佳答案

已解决。

上半场是我的疏忽。下半场……好吧,我不知道哪里出了问题。这并不是真正的错误或不兼容,而是一些非常不方便、断断续续且难以弄清楚的事情。先是一个总结,然后是给关心的人的篇幅解释:

尽管有错误消息建议,但这不是概念模型 (CSDL) 的问题,而是间歇性重新创建自身的列映射问题。

概念模型是使用 EdmxWriter 构建的,用于解析 DbContext 及其底层部分。

然后该模型用于生成 SQL 脚本以将模式推送到新数据库。诀窍是,数据库是 Oracle。

Oracle 是一个婴儿,不接受长列名。因此,必须修改生成的 EDMX 和 SQL 脚本,以构建部分概念模型并将其映射到截断的列名称。

其实没什么大不了的。它工作正常。那么哪里出了问题呢?

Oracle 不支持“代码优先”。即使它是手动完成的,使用 EdmxWriter 在 Oracle 眼中构成了代码优先的方法。因此,当第一个 EDMX 模式被解析时,它对 bool 映射感到不满。解决方案是暂时从我的 C# 模型中删除 bool,将它们手动添加到 EDMX 并制作 Oracle 建议的 web.config 映射(将 bool 映射到 NUMBER(1,0))。

一切又变得很正常了。但为什么它会反复出现?

在整个开发过程的不同时间,协议(protocol)的某些端点(C#、EDMX 或 Oracle)会发生变化。每次,列似乎都自动重新映射,而我却没有意识到。如果 EDMX 模型是从 Oracle 刷新的,则映射指向不存在的 C# 属性(短列名称)。如果模型是从 C# 代码刷新的,则映射不会保留,它们会尝试映射到 Oracle 中没有的长列名。

这种方法(混合代码优先和模型优先)的缺点是,如果我想继续管理自己的模型并处理 Oracle 小宝贝态度所需的定制,我必须非常小心并监控 EDMX 文件。

关于c# - EntitySet System.InvalidOperationException - "the entity type is not part of the model for the current context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13634819/

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