EntitySet的System.InvalidOperationException - "实体类型是不是该机型为当前上下文和部分地方; [英] EntitySet System.InvalidOperationException - "the entity type is not part of the model for the current context"

查看:1467
本文介绍了EntitySet的System.InvalidOperationException - "实体类型是不是该机型为当前上下文和部分地方;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体类型<&类名GT;是不是为当前上下文 - 和 - 的 EF 4.1代码第一次错误 - 实体类型SOMETYPE是不是该机型为当前上下文有类似的问题的一部分,但它们代码第一只能看,随着更简单的数据模型和地址连接字符串和映射问题。请这一个密切关注。

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();
}



抛出异常检索饮料系列:

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; }
}



我的经验是用模型优先方法。该文件EDMX建那么波苏斯。

My experience was in using a Model First methodology. The EDMX file was built then the POCOs.

在连接字符串的元数据部分映射到解析EDMX资源( =元数据资源://*/Models.MealsModels.csdl |高分辨率://*/Models.MealsModels.ssdl |高分辨率。//*/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;).

我检查EDMX文件的基本XML显示目前在概念和存储模型的所有实体,并且都完全映射。 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:


  1. 检索餐饮抛出 MSL,错误2062没有可供的EntitySet和AssociationSet在EntityContainer相关的实例指定映射

检索饮料继续抛出的实体类型Drinkis不是模型针对当前的上下文的一部分。

Retrieving Drinks continues to throw The entity type Drinkis not part of the model for the current context.

预计膳食抛出的错误,我核爆的映射和专卖店模式 - 检查 _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 接近让我发现,饮料 - > 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正确地定义了实体类型<$连接字符串C $ C>饮料, 为什么在地狱又何尝不是上下文的一部分?

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?

关于唯一不同的饮料,我可以看到的是,它不与任何其他实体,并且没有关联...

The only thing different about Drink that I can see is that it is not related to any other entities, and has no associations...

推荐答案

解决。

上半年是我的疏忽。下半场......好吧我没有对什么是错一个字。这是不是一个真正的错误,或者不兼容的事,但非常不方便,间歇性和很难搞清楚。首先一个摘要,然后将长度解释为那些谁关心:

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 构成了甲骨文的眼睛代码优先方法。因此,当第一个EDMX架构被解析,它放荡约布尔映射。解决的办法是从我的C#车型暂时删除的bool,他们手动添加到EDMX,使web.config中映射的Oracle建议(映射布尔 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 - &QUOT;实体类型是不是该机型为当前上下文和部分地方;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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