Error in DbContext after adding a column to table (ASP.NET/EF Core)

asp.net-core entity-framework-core model

Question

All was working fine until I added a new column to my table. I could not find how to update the model class, so I added the column myself in model. But now, when I run my application, I get error: "Object reference not set to an instance of an object."

During debug, I found that it is the problem with creation of instance of my dbcontext:

MyDbContext context = new MyDbContext(db);

The context object is Not fetching rows from table and is giving this Object reference error.

If I remove the new column from Model class then it works fine. That suggests that my database and model is Not in sync and it seems that the application is using old table design.

I am also trying to use CLI commands to update database but facing difficulties in that as well.

Can anyone suggest how to make model class and table design in sync once a table column is added? I am using ASP.NET Core and EF Core.

Thank you.

1
0
12/31/2016 1:10:25 AM

Popular Answer

put try catch to find the exact problem.

This problem occurs because of the difference between schema of database and changed schema at code level. you need to add this line.

Database.SetInitializer<yourDbName>(new DropCreateDatabaseIfModelChanges<yourDbName>());

It will synchronize your database.

0
8/18/2017 4:33:17 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow