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

18,981

Solution 1

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

Solution 2

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.

Share:
18,981
fhnaseer
Author by

fhnaseer

C#, WPF, MVVM, Entity Framework, WCF, TDD, Unit-Testing, Agile, Scrum,

Updated on June 04, 2022

Comments

  • fhnaseer
    fhnaseer almost 2 years

    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
    fhnaseer about 11 years
    Sorry, I didn't get it what you are trying to say.
  • MikroDel
    MikroDel about 11 years
    @FaisalHafeez - I have updated my answer. Is it clear for you now?
  • MikroDel
    MikroDel about 11 years
    @FaisalHafeez - what is your problem with my answer? Have you tried the action I recommend to you?
  • fhnaseer
    fhnaseer about 11 years
    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.
  • MikroDel
    MikroDel about 11 years
    @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.
  • Gert Arnold
    Gert Arnold about 11 years
    @MikroDel This is about database first, not code first, so your only sidetracking the OP.
  • MikroDel
    MikroDel about 11 years
    @FaisalHafeez - I think with you tag "poco" you mean code first.
  • MikroDel
    MikroDel about 11 years
    @FaisalHafeez - the other way as you say "database first" - isnt it better for you to change it on database side?
  • fhnaseer
    fhnaseer about 11 years
    I am new to Entity Framework. Well I edited my edmx file and changes some values. It is working fine now.
  • MikroDel
    MikroDel about 11 years
    @FaisalHafeez - so mark my post as an answer and upvote it please. =) Cause this discussion is also part of the answer.
  • Gert Arnold
    Gert Arnold about 11 years
    Huh? Accept, upvote? You only confused him. The right thing to do is remove your answer.
  • MikroDel
    MikroDel about 11 years
    @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.
  • fhnaseer
    fhnaseer about 11 years
    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.
  • TheLostBrain
    TheLostBrain over 5 years
    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!