2

I know This question is duplicate I found one solution too but this is four year old question and I think in these years the EntityFramework is on different level so I want to ask this question again. Here is the answer which I found and try to apply on entity Framework 5.0 but does not work

The model backing the <Database> context has changed since the database was created

here is my entity model classes first one is this

    public class Student
{
    public int StudentId {get; set;}
    [StringLength(250),Required]
    public string StudentName {get; set;}
    [StringLength(20),Required]
    public string Standard {get; set; }
    [StringLength(250),Required]
    public string Address {get; set; }
    [StringLength(250),Required]
    public string Emailid {get; set; }
    [StringLength(12),Required]
    public string Phoneno {get; set; }

}

and Second Model Class is this

    public class Marks
{

    public int MarksID { get; set; }
    public int StudentId { get; set; }
    public int English { get; set; }
    public int Maths { get; set; }
    public int Science { get; set; }
    public int SocialStudy { get; set; }
    public int Hindi { get; set; }
    public int Total { get; set; }
    public Student student { get; set; }

}

and this is my context class

 public class DBContext:DbContext
{
    public DBContext()
    {
        Database.SetInitializer<DbContext>(new DropCreateDatabaseAlways<DbContext>());
    }

    public DbSet<Student> TbStudent { get; set; }
    public DbSet<Marks> TbMarks { get; set; }
}

I run this code and Getting this error

The model backing the 'DBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Please expert help me regarding this issue

Community
  • 1
  • 1

1 Answers1

0

Code first migration will help you resolving the case.But to proceed in your way to initialize the database you must replace DbContext with DBContext(i.e with your context class) in the Database.SetInitializer function like this.

  public class DBContext:DbContext
  {
       public DBContext()
       {
           Database.SetInitializer<DBContext>(new DropCreateDatabaseAlways<DBContext>());
       }
   }

Hope this helps.

user3603255
  • 260
  • 1
  • 3
  • 12
  • 1
    how stupid I am. I am using Dbcontext instead of my Class such a minor mistake that I did not noticed.. Now I am getting angry at myself for wasting 2 hours for searching such kind of small mistake thanks ... :) – HarSimran Kaur Oct 13 '14 at 10:52
  • @HarSimranKaur this is EXACTLY what was happening to me for almost an hour. Thank you both! – Can Poyrazoğlu Nov 27 '15 at 22:22