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

Related questions

The entity type <classname> is not part of the model for the current context -and- EF 4.1 Code The first error. The SomeType object type is not part of the model for the current context. are similar questions, but they are only in terms of "code", with much simpler data models, as well as a connection address bar and display. Look carefully at that.

Symptoms

// HomeController.cs public ActionResult Index() { var _db = new MealsContext(); var m = _db.Meals.ToList(); var d = _db.Drinks.ToList(); return View(); } 

An exception was thrown from the Drinks collection:

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

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

Yes, I know that in the real world, the relationship between meat and vegetables with dishes is likely to be โ€œMany for many,โ€ but don't dwell on it here.

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

My experience was using the Model First methodology. The EDMX file was then created by POCOs.

The connection string contains a metadata section that maps to the analyzed EDMX resources ( metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl; ).

I looked at the basic XML of the EDMX file, which shows all the entities present in the Conceptual and Store models, and they are all completely mapped. WTF?

Troubleshooting

First, we tried to completely get rid of the storage and display of EDMX data ( SSDL and MSL sections). Fire, and now there are two exceptions:

  • Extract Meals throws MSL, error 2062 No mapping specified for instance of the EntitySet and AssociationSet in the EntityContainer .

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

The error caused by Meals is expected, I displayed the mappings and the storage model - examining _db shows me that the property Meals โ†’ InternalSet โ†’ EntitySet is correct, it just does not display.

The error created by Drinks is where I get stuck. A closer _db shows me that Drinks InternalSet EntitySet throws a SystemInvalidOperation exception, which indicates that the object is not in the context of the model.

Here's what the EDMX 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 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> 

Question

If DbContext has all the DbSet properties and consumes a connection string that includes metadata for the model, which CSDL correctly defines the Drink entity type , why the hell is this not part of the context?

The only difference in Drink that I see is that it is not associated with any other objects and has no associations ...

+6
c # entity-framework dbcontext ef-model-first csdl
Nov 29
source share
2 answers

solvable.

The first half was my oversight. The second half ... well, I donโ€™t have a word about what was wrong. Actually, this is not a mistake, or incompatibility, but something very uncomfortable, intermittent and difficult to understand. First a summary, and then an explanation of the length for those who care:

Despite the error message suggestions, this is not a conceptual model problem (CSDL), but a column matching problem that recreated itself intermittently.

The conceptual model was built using EdmxWriter to parse DbContext and its main parts.

The model was then used to generate SQL scripts to translate the schema into a new database. The trick is that the database is Oracle.

Oracle is a child and does not accept long column names. Thus, the generated EDMX and SQL scripts had to be modified to create and map parts of the conceptual model to shortened column names.

Not a very big deal. It is working fine. So where did it all go wrong?

Oracle does not support "code first." And although this was done manually, using EdmxWriter is a code approach in Oracle. Therefore, when the first EDMX schema was parsed, it was a binar about logical mappings. The solution was to temporarily remove the bools from my C # models, add them to EDMX manually and make the Oracle web.config mapping (matching bool to NUMBER(1,0) ).

All groovy again. But why does he keep repeating?

At various times throughout the development process, some end of the agreement โ€” either C #, EDMX, or Oracle โ€” changes. And each time, it seems, the columns were automatically reassigned, and I did not know. If the EDMX model was upgraded from Oracle, the mappings pointed to C # properties that were not there (short column names). If the model was updated from C # code, the mappings were not saved, and they tried to match long column names that were not in Oracle.

A bummer with this approach (first, the first hybrid code and model) - if I want to continue to manage my own models and process the settings necessary for a little relationship with the child, I have to be very careful and keep an eye on the EDMX file trait.

+4
Dec 03 '12 at 20:14
source share

You need to specify mappings for your objects:

 public class MealsContext: DbContext { public MealsContext() : base("ConnectionString") protected override void OnModelCreating(DbModelBuilder modelBuilder) { // mappings } public DbSet<Meal> Meals{ get; set; } public DbSet<Meat> Meats{ get; set; } public DbSet<Vegetable> Vegetables { get; set; } public DbSet<Drink> Drinks{ get; set; } } 

I had the same problem until I started using Entity Framework Power Tools

Using it, you can create clear objects, such as business objects and mapping classes. Good article that helped me create an amazing level of data access: Engineer reverse code first

0
Nov 29 '12 at 22:40
source share



All Articles