Part 5 How to handle model changes in entity framework

preview_player
Показать описание
Text version of the video

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.

Slides

Entity Framework - All Text Articles

Entity Framework - All Slides

Entity Framework Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

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.

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.
Рекомендации по теме
Комментарии
Автор

Hi Venkat,
You are a really good instructor.Keep up the good work and thank you for all your videos. I have a question. When do we make entity framework make our database because clearly if we already have an application running we have a lot of data that we don't want to lose?

ariajafarian
Автор

can we update or alter the table without dropping or deleting the database and data.while we maintaining the project we may face situation to add the fields without disturbing the data.pls explain that, for that code first approach is suitable

ohmprakash
Автор

Dear Venkat,
This is the only way to add a table field?
Drop and re-create the database?
Thank you

georgekarakasidis
Автор

hi Venkat,
Can You Please suggest for the same in Winform? how to handle the model changes in Entity framework

prayogtutorialpoint
Автор

All of your turorials are great dude very helpful, thank you so much

ialimijororakotoniaina
Автор

I love your videos! They have been very helpful. In this video you set the application up so it will drop and recreate the database if you've made changes. I like this because I'm just getting started with Entity Framework Core 5 and .net Core. Unfortunately, with WPF there is no longer a Startup. How can I set my WPF to do this as I'm developing the database, etc. using the code first model and MVVM? I'm using PM to create the first instance/update/etc. but I want to clear everything as I make changes to the database and start fresh each time.

kenschneider
Автор

hi venkat, lets say if we have important data in the table, we need to retain those data without dropping it when model changes, how to handle these case.

dileepc.p
Автор

Why do we use the POCO classes (EF) in our projects at all ?? why are POCO classes so famous ? Please help thru some good article of urs.
Is designing the database from T-SQL Client not better than this new method of creating database from POCO ?
Why ? Why ? Why ? pLEASE HELP VENKAT

sww
Автор

This solution is totally unworkable in a commercial environment - what DB Administrator is going to allow as application to kill all the data every time that application starts?  No-one.  Epic fail !

sajjie
Автор

So what if the Global.asax file is missing? I can not find it in my solution, when trying to add a new item :O

nataschabjerning
Автор

Hi Sir, Thank you for uploading all tutorial video and i go through it so many time and that is really help me for my career and interviews. I was go through all EE tutorial and i have quick question regarding code first approach like if we change class then need to drop database and it will create automatically but we can not drop data base all the time even if i set in global file so is there any solution for that without dropping data all the time and move changes to database. ? please advise sir. Thanks a lot!!

kaushikbhadani
Автор

Sir if my application is running mode what to do sir?
If drop the table then all existing data will delete
plz help me sir

alokkumarsahoo
Автор

Vekat Sir how to close the connection of edmx explicitly ?

saurabhchauhan
Автор

venkat brother how to fix this problem Login failed for user 'IIS APPPOOL\\DefaultAppPool. Plz help me. I find this problem when working with your asp.net mvc tutorial part 8. 

IftekherSunny
Автор

What about the data stored in the database by this we can loose all the data.

Itsfunn
Автор

good night sir

How to create run time class.

Devendrasingh-uoch
Автор

to be able to change or add column or delete without drop the database please check that link from Microsoft which describe the database migration

abdelmoneimmohamed
Автор

If you like me need to do this in .NET CORE then Global.asax wont work. To this instead:

If you have created the migrations, you could execute them in the Startup.cs as follows.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
using (var serviceScope =
{
var context =
context.Database.Migrate();
}

Read more:

larrevick