8

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.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112

4 Answers4

14

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

Slava
  • 3,445
  • 1
  • 20
  • 16
  • I just had this very issue with a slight twist. In my situation the EF related items were in a separate library project. The connection string in the app.config was getting updated correctly. However, I had failed to manually update the copy I had in the main project's web.config file... which was an older copy of the connection string with (now) incorrect metadata, etc. Thanks! – TheLostBrain Jan 17 '19 at 21:41
1

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.

MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • Sorry, I didn't get it what you are trying to say. – fhnaseer Apr 15 '13 at 09:52
  • 1
    @FaisalHafeez - I have updated my answer. Is it clear for you now? – MikroDel Apr 15 '13 at 09:53
  • @FaisalHafeez - what is your problem with my answer? Have you tried the action I recommend to you? – MikroDel Apr 15 '13 at 09:59
  • 1
    The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. – fhnaseer Apr 15 '13 at 10:18
  • @FaisalHafeez - 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. – MikroDel Apr 15 '13 at 10:43
  • @MikroDel This is about database first, not code first, so your only sidetracking the OP. – Gert Arnold Apr 15 '13 at 10:46
  • @FaisalHafeez - I think with you tag "poco" you mean code first. – MikroDel Apr 15 '13 at 10:47
  • @FaisalHafeez - the other way as you say "database first" - isnt it better for you to change it on database side? – MikroDel Apr 15 '13 at 10:48
  • 1
    I am new to Entity Framework. Well I edited my edmx file and changes some values. It is working fine now. – fhnaseer Apr 15 '13 at 10:50
  • @FaisalHafeez - so mark my post as an answer and upvote it please. =) Cause this discussion is also part of the answer. – MikroDel Apr 15 '13 at 11:09
  • 1
    Huh? Accept, upvote? You only confused him. The right thing to do is remove your answer. – Gert Arnold Apr 15 '13 at 12:44
  • @GertArnold - I have post - and in a discussion with me Faisal Hafeez have receive the answer from me. If you think my answer should be removed - flag it to remove. – MikroDel Apr 15 '13 at 12:51
0

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

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • You asked me to run some commands (which didn't worked for me). And what I did edit entity name. The mistake which I did was that I changed class name of generated files but didn't told model to use new class, so it was not able to find new class. – fhnaseer Apr 16 '13 at 06:28
0

Many tutorials ask to make all these extra classes that are not needed. Basically all you need to do, to make the entity framework work is create the model and then create the object in your controler.

Example Model: myEntity.edmx Example Controller:

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 read: "myEntityContext" is not part of the model" it was true because I was creating an extra class with the name "myEntityContext" per the tutorials.

When you try to create a strong view with the context that you create it will blow up, because it tries to associate a class that does not exists in the model. So by deleting all the extra DAL and Model Context, creating a new view using the Entity.context that shows up in the Strong view menu everything should work fine.

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

Community
  • 1
  • 1
JEuvin
  • 866
  • 1
  • 12
  • 31