Part 5 How to handle model changes in entity framework

kudvenkat
kudvenkat
184.2 هزار بار بازدید - 11 سال پیش - Text version of the video
Text version of the video
http://csharp-video-tutorials.blogspo...

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
@aarvikitchen5572

Slides
http://csharp-video-tutorials.blogspo...

Entity Framework - All Text Articles
http://csharp-video-tutorials.blogspo...

Entity Framework - All Slides
http://csharp-video-tutorials.blogspo...

Entity Framework Playlist
Entity Framework Tutorial

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
https://www.youtube.com/user/kudvenka...


In this video we will discuss, how to handle model changes after the database is already created. This is continuation to Part 4. Please watch Part 4 before proceeding.

Based on Employee class Entity Framework has auto-generated table tblEmployees.

By default, an error will be thrown if the model changes after the database is created. This is because the Model and Database are no longer in sync. To check if the model has changed since the database was created, entity framework uses _MigrationHistory table that is auto-generated.

To fix this error, we have to tell entity framework what to do when the model changes. Include the following code in Global.asax file. Here, we are telling the entity framework to drop and recreate database every time the model changes.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges[EmployeeDBContext]());

Another option you have here is, to drop and recreate the database always. To drop and recreate the database always we would change the code in Application_Start() method as shown below.
Database.SetInitializer(new DropCreateDatabaseAlways[EmployeeDBContext]());

Please Note: Database class is present in System.Data.Entity namespace.

Run the application, and notice that the database is dropped and recreated. But the webform does not display any data, as there is no data in Departments and tblEmployees tables. For now let's manually populate the tables using SQL script.

Referesh the webform. Notice that JobTitle is not displayed on the WebForm. To fix this add a boundfield to the GridView control that displays Employees details.
11 سال پیش در تاریخ 1393/02/18 منتشر شده است.
184,287 بـار بازدید شده
... بیشتر