The entity type [Name] is not part of the model for the current context

fhnaseer picture fhnaseer · Apr 15, 2013 · Viewed 18k times · Source

I create a model using EF and generated its context using DbContext 5.X generator. Now I renamed class name of one of my entities. Now when I run my code I get "The entity type Student2 is not part of the model for the current context." error.

var context = new MyEntities(connectionString);
foreach(var student in context.Students)
{
    Console.WriteLine(class.Name.ToString());
}

In my data context.

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    // public DbSet<Student> Students { get; set; } -> Origional
    public DbSet<Student2> Student { get; set; } // I renamed Student to Student2
}

How to fix this? I need to rename my class due to some conflicts.

Answer

Slava picture Slava · Dec 17, 2013

I had the same issue when i had wrong metadata in connection string. Try to recreate connection string in app.config.