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

c# csdl dbcontext ef-model-first entity-framework

Question

Similar Issues

Similar questions include The model for the current context does not include the entity type classname>. and Code EF 4.1 Initial error: Entity type SomeType is not included in the model for this context., although they only use a "code first" perspective, have considerably simpler data models, and deal with connection string and mapping concerns. Please pay close attention to this one.

Symptom

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

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

    return View();
}

When attempting to retrieve theDrinks 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; }
}

Don't get too hung up on the fact that, in the real world, the relationship between meat and vegetables in meals would probably be Many-to-Many.

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

Using a Model First methodology was my experience. The POCOs were constructed after the EDMX file.

The metadata section of the connection string corresponds to the parsed EDMX resources (metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl; ).

All of the entities present in the Conceptual and Store models are present and fully mapped, according to my analysis of the EDMX file's underlying XML. WTF?

Troubleshooting

The initial attempt was to totally remove the store and EDMX data mapping (theSSDL and MSL sections). Fire away, with the following two exceptions:

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

  2. Retrieving Drinks carries on throwingThe entity type Drinkis not part of the model for the current context .

The mistake made byMeals I destroyed the mappings and store model as was intended — looking_db proves to me thatMeals -> InternalSet -> EntitySet Property is accurate; it is merely not mapped.

The mistake made byDrinks is where I'm at a loss. Examining_db closer shows me thatDrinks -> InternalSet -> EntitySet hurls theSystemInvalidOperation The entity is not in the model context, according to the exception.

The XML representation of the EDMX's CSDL is shown below:

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

Question

If theDbContext has everyDbSet a model whose CSDL accurately describes the entity type, and is consuming a connection string that includes metadata for that model.Drink , 163-zzz zzz

The lone distinction betweenDrink that I can see is that it is not related to any other entities, and has no associations...

1
6
5/23/2017 10:27:45 AM

Accepted Answer

ZZZ_tmp
4
1/12/2015 2:57:25 PM

Popular Answer

The mappings for your entities must be specified:

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 experienced the same issue before to using Powerful Entity Framework Tools.

It allows you to construct distinct entities such as business objects and mapping classes. Excellent article that enabled me to develop an incredible data access layer: Code First: Reverse Engineering



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow