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

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

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 it? I need to rename my class due to some conflicts.

+7
source share
4 answers

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

+12
source

Use Add-Migration

This is an example:

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

and Update-Database, for example:

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

In Visual Studio you can use the Package Manager Console for it. As the default project, you should choose your Entity Framework project - if there are many.

+1
source

Well here is the solution. Open the UI of the Model.edmx file, and there change the student name to Student2. This will create new files and context in which the student will be replaced by Student2.

0
source

Many textbooks ask you to do all these extra classes that are not needed. Basically, all you need to do to work with the entity infrastructure is creating a model and then creating an object in your controller.

Model example: myEntity.edmx Controller example:

 public class HomeController : Controller { myEntity db = new myEntity(); public ActionResult Index() { return View(db.myTable.ToList()); } } 

Everything else is in the entity model, so when the error reads: "myEntityContext" is not part of the model "this was true, because I created an additional class called" myEntityContext "for tutorials.

When you try to create a strong representation with the context being created, it will explode because it is trying to link a class that does not exist in the model. Thus, removing all the additional DALs and the model context, creating a new view using Entity.context, which is displayed in the Strong View menu, everything should work fine.

I had the same problem and posted what I did to fix it

0
source

All Articles