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.
I had the same issue when i had wrong metadata in connection string. Try to recreate connection string in app.config.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With