Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 300
fhnaseer Avatar asked Apr 15 '13 09:04

fhnaseer


2 Answers

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

like image 73
Slava Avatar answered Sep 22 '22 13:09

Slava


Use Add-Migration

This is the sample:

Add-Migration "Muster" -ConnectionString "Data Source=.;" -ConnectionProviderName System.Data.SqlClient

and Update-Database, like this:

Update-Database -ConnectionString "Data Source=.;" -ConnectionProviderName System.Data.SqlClient

In Visual Studio you can use Package Manager Console for it. As a default Project you should choose your Entity Framework project - if you have many.

like image 36
MikroDel Avatar answered Sep 24 '22 13:09

MikroDel