Cenk-1028 avatar image
0 Votes"
Cenk-1028 asked JackJJun-MSFT commented

Adding New Property to an existing model


I have implemented an web API with the code first approach. Now I need to add a property to some of the models. I alter the DB table and add a field, I got this error:

System.InvalidOperationException HResult=0x80131509 Message=The model backing the 'GameContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( Source=<Cannot evaluate the exception source> StackTrace: <Cannot evaluate the exception stack trace>

I googled and came across adding below to the global.asax:


I wonder is it OK to use this on the production? Any suggestions would be great.

Thanks in advance.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JackJJun-MSFT thank you for your answer. Assume I did what you have said above in my development environment. How can I adapt it to my production? Code is already changed and I need to manually alter the table in my DB. Any ideas?

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT commented

@Cenk-1028, based on my test, you could try the following steps to add a new property to a model and the related database.

First, Please add the property in the model class.

  public class Student
             public int ID { get; set; }
             public string Name { get; set; }
             public int Age { get; set; }
             public string Address { get; set; }
             public int Score { get; set; }    // Added property

Second, Please execute the following commands in sequence in the Package Console below.

 Add-Migration AddFileld

Finally, you could see the results in the code and database.


 public partial class AddFileld : DbMigration
         public override void Up()
             AddColumn("dbo.Students", "Score", c => c.Int(nullable: false));
         public override void Down()
             DropColumn("dbo.Students", "Score");



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

image.png (9.2 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Cenk-1028, there is no need to manually alter the table in your db, after executing the above commands the Score Field can be added to the database automatically.

0 Votes 0 ·

@JackJJun-MSFT But I don't get it, I am doing it on my local development machine. How this additional field will be added to the database on my production machine?

0 Votes 0 ·

@Cenk-1028, If you want to add the field to other machine database, we need to specify both the project name in Package Console and the specified database in code.


  public StudentContext() : base(@"Connstr")
         { }


0 Votes 0 ·
image.png (14.9 KiB)
Show more comments