Back to Browse

Part 5 How to handle model changes in entity framework

187.2K views
May 8, 2014
7:37

Text version of the video http://csharp-video-tutorials.blogspot.com/2014/05/part-5-how-to-handle-model-changes-in.html 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. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Slides http://csharp-video-tutorials.blogspot.com/2014/05/part-5-how-to-handle-model-changes-in_8.html Entity Framework - All Text Articles http://csharp-video-tutorials.blogspot.com/2014/05/entity-framework-tutorial.html Entity Framework - All Slides http://csharp-video-tutorials.blogspot.com/2014/05/entity-framework-tutorial-slides.html Entity Framework Playlist https://www.youtube.com/playlist?list=PL6n9fhu94yhUPBSX-E2aJCnCR3-_6zBZx Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd 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.

Download

0 formats

No download links available.

Part 5 How to handle model changes in entity framework | NatokHD