EntitySet System.InvalidOperationException - “实体类型不是当前上下文的模型的一部分” [英] EntitySet System.InvalidOperationException - "the entity type is not part of the model for the current context"
问题描述
实体类型< classname>不是当前上下文模型的一部分 - 和 - 是类似的问题,但它们是代码第一透视,使用更简单的数据模型,并解决连接字符串和映射问题。请仔细查看这一个。
The entity type <classname> is not part of the model for the current context -and- EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context are similar questions but they are "code first" perspective only, with much simpler data models, and address connection string and mapping issues. Please look closely at this one.
// HomeController.cs
public ActionResult Index()
{
var _db = new MealsContext();
var m = _db.Meals.ToList();
var d = _db.Drinks.ToList();
return View();
}
异常被抛出检索饮料
collection:
Exception is thrown retrieving the Drinks
collection:
The entity type Drink is not part of the model for the current context.
代码
Code
// 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; }
}
是的,我在现实世界中知道肉之间的关系和蔬菜与膳食可能是多对多,但不要挂在这里。
// 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; }
}
我的经验是使用Model First方法。 EDMX文件被创建,然后是POCO。
My experience was in using a Model First methodology. The EDMX file was built then the POCOs.
连接字符串是映射到已解析的EDMX资源的元数据部分( metadata = res://*/Models.MealsModels.csdl | res://*/Models.MealsModels.ssdl | res://*/Models.MealsModels.msl;
)。
In the connection string is the metadata section that maps to the parsed EDMX resources (metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl;
).
I检查了EDMX文件的底层XML,显示了Conceptual和Store模型中存在的所有实体,并且都完全映射。 WTF?
I examined the underlying XML of the EDMX file shows all entities present in Conceptual and Store models, and all are fully mapped. WTF?
第一次尝试是完全摆脱存储和映射EDMX数据( SSDL
和 MSL
部分)。现在有两个例外:
The first tried was to completely get rid of the store and mapping EDMX data (the SSDL
and MSL
sections). Fire away, and now there are two exceptions:
-
检索
餐食
throwsMSL,error 2062在EntityContainer
中没有指定实例EntitySet和AssociationSet的映射。
Retrieving
Meals
throwsMSL, error 2062 No mapping specified for instance of the EntitySet and AssociationSet in the EntityContainer
.
饮料
继续抛出实体类型Drinkis不是当前上下文模型的一部分
。
预计由 Meals
抛出的错误,我将映射和商店模型 - 检查 _db
向我显示餐食
- > InternalSet
- > EntitySet
属性是正确的,只是没有映射。
The error thrown by Meals
is expected, I nuked the mappings and store model -- examining _db
shows me that Meals
-> InternalSet
-> EntitySet
property is correct, just not mapped.
是我被卡住的地方。检查 _db
close显示我饮料
- > InternalSet
> EntitySet
抛出说明实体不在模型上下文中的 SystemInvalidOperation
异常。
The error thrown by Drinks
is where I am stuck. Examining _db
closer shows me that Drinks
-> InternalSet
-> EntitySet
throws the SystemInvalidOperation
exception that states the entity is not in the model context.
以下是EDMX的CSDL格式的XML格式:
Here's what the EDMX's CSDL looks like in XML format:
<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
, 为什么在地狱里不是上下文的一部分?
Question
If the DbContext
has all the DbSet
properties and is consuming a connection string that includes metadata for a model who's CSDL correctly defines the entity type Drink
, why in the hell is it not part of the context?
我可以看到的 Drink
唯一不同的是它与任何其他实体无关,没有关联...
The only thing different about Drink
that I can see is that it is not related to any other entities, and has no associations...
推荐答案
解决。
上半年是我的监督。下半场,我没有任何错误的字眼。它不是真的是一个bug,或不兼容,但是非常不方便,间歇性和难以弄清楚的东西。首先是一个摘要,然后对那些关心的人的长度解释:
The first half was my oversight. The second half... well I don't have a word for what was wrong. It is not really a bug, or incompatibility, but something very inconvenient, intermittent and hard to figure out. First a summary, and then the length explanation for those who care:
概念模型是使用 EdmxWriter
构建的,以解析 DbContext
及其底层。
The conceptual model was built using the EdmxWriter
to parse the DbContext
and its underlying pieces.
然后,该模型用于生成SQL脚本以将模式推送到新数据库。诀窍是,数据库是Oracle。
The model then was used to generate SQL scripts to push the schema to a new database. The trick is, the database is Oracle.
Oracle是一个宝贝,不接受长列名称。因此,生成的EDMX和SQL脚本必须进行修改,以构建概念模型的部分并将其映射到截断的列名。
Oracle is a baby and does not accept long column names. So the generated EDMX and SQL Scripts had to be modified to build and map parts of the conceptual model to truncated column names.
不是一件大事。工作正常那么事情出错了?
Not really a big deal. It works fine. So where did things go wrong?
Oracle不支持代码优先。即使手动完成,使用 EdmxWriter
构成了Oracle眼中的代码优先方法。所以当第一个EDMX模式被解析时,它扼杀了布尔映射。解决方案是暂时从我的C#模型中删除bools,将它们手动添加到EDMX中,并使Oracle建议的web.config映射(映射 bool
到 NUMBER(1,0)
)。
Oracle does not support "code first". And even though it was done manually, using the EdmxWriter
constitutes a code-first approach in Oracle's eyes. So when the first EDMX schema was parsed, it bitched about boolean mappings. The solution was to temporarily remove the bools from my C# models, add them to the EDMX manually and make the web.config mapping Oracle suggests (mapping bool
to NUMBER(1,0)
).
一切都重新开始。但是为什么它会不断重演?
Everything is groovy again. But why does it keep reoccurring?
在整个开发过程的不同时期,协议的一些目的 - 即C#,EDMX或Oracle - 被改变。而且每次看来,这些列被自动重新映射,并且我不知道。如果EDMX模型从Oracle刷新,则映射指向不存在的C#属性(短列名称)。如果模型从C#代码刷新,则映射不会被保留,并且尝试映射到不在Oracle中的长列名称。
使用这种方法(首先是混合代码,首先是模型)如果我想继续管理我自己的模型并处理Oracle的小宝宝态度所需的自定义,我必须非常小心并监视地狱在EDMX文件中。
The bummer with this approach (sort of hybrid code first and model first) is if I want to continue managing my own models and handle customizations necessary for Oracle's little baby attitude, I have to be very careful and monitor the hell out of the EDMX file.
这篇关于EntitySet System.InvalidOperationException - “实体类型不是当前上下文的模型的一部分”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!