Keeping domain models and database schema in sync in asp net core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is pad 52 of asp.net core story all in this video we'll discuss how to keep our application domain models and database schema in sync using migrations in asp.net core along the way we'll also discuss how to add/remove and apply a migration to our underlying database we'll also discuss how to undo a migration that is already applied and bring the database schema to its previous state finally we'll also discuss the significance of model snapshot class and underscore underscore EF migration history table as we develop our application and add new features our application domain class has changed when the domain class has changed the corresponding changes have to be made to the underlying database schema as well otherwise the database schema goes out of sync and the application does not work as expected however it's important to remember not to manually make these changes to the database schema we use migrations to keep the database schema in sync as the application domain classes change let's understand this with an example if we take a look at our create employee form notice at the moment we are only capturing name email Department we also want to be able to upload employee photo so for that we need a new field we'll call it photo path and then provide a file upload control here using which the user can upload a photo in the models folder here is our employee model class at the moment we have ID name email and Department properties we now need a new property to capture the photo part of the employee the datatype of the property is going to be string and let's name the property photo part now if we take a look at the underlying employees database table notice we only have columns for ID name email and Department properties we do not have a corresponding column in this table for the new property that we have just added which is photo path now to bring our database schema in sync with our application model classes in a different book or we use migrations before we add in new migration let's make a deliberate typographical error with this property let's remove the letter H in the photo path property and then add a new migration to add a new migration we use package manager console in Visual Studio to get a package manager console click on view other windows and then package manager console to add a new migration we use add migration command let's name this migration add photo path column notice a new file that contains migration code is added and you can find this file in the migrations folder notice the file name contains a timestamp and underscore and then the name that we provided when we added this migration the class in this file has the same name as all migration and this class contains two methods up and down the up method contains the code to apply the changes that we made to the model class to our underlying database table we added photo birth property to our employee model class so the code in this up method is adding photo path column to the underlying employees table down method we'll undo this changes up added adds this column down method drop start for the path column when we add a new migration that command in addition to adding this file which contains our migration code it also updates this file app dbcontext model snapshot dot CS notice the name of this file is made up of our application DB context class our application DB context class is AB DB context so AB DB context and then model snapshot as the name implies this file contains the snapshot of your current model this file is created when the first migration is added and updated with each subsequent migration EF Kol migrations API uses this file to determine what has changed when adding the next migration if we take a look at the contents of this file notice it contains a snapshot of our current model at the moment our employee model class contains these properties ID Department email name and photo path photo part is the new property that we just added now when we add another new migration EF cor uses this file to determine what has changed let's actually add another new migration but before that let's add a new property to our employee model class let's name this property some property now let's add another migration let's name this migration add some property notice our new migration file is generated and we also have a warning here of a model snapshot file AB dbcontext model snapshot dot C S has been changed externally do you want to reload it remember every time we add a new migration the model snapshot file is updated with our current application model snapshot since this file is open in the editor and since it has been modified it's asking us if we want to reload it yes we do want to reload it at this point if we take a look at our model snapshot file notice it has our new property some property at the moment we've got two migrations add photo path column add some property now to remove a migration we use remove migration command notice the message here removing migration add some property so it removes that migration file and it also reverted the model snapshot file since the model snapshot file has changed it's again asking us if we want to reload it yes we do want to reload it now if we look in a migrations folder notice the migration file under schoo add some property is removed from here also the property some property is removed from the model snapshot file next we want to remove this migration as well add photo path column so from our package manager console window let's execute remove migration command once again there we go the migration underscore add photo path column is removed the model snapshot file is also updated and if we look in the migrations folder the respective migration file is deleted now let's try to remove this migration as well all the employees seed data so from the package manager console window let's execute remove migration command one more time notice they are not able to remove this migration we have an error message the migration alter employees see data has already been applied to the database revert it and try again and if we look in the migrations folder they still have that migration file alter employees see data so the important point to keep in mind is remove migration command only removes one migration at a time and that do only the latest migration that is not yet applied to the database if all the migrations are already applied executing this remove migration command throws an exception the migration has already been applied to the database revert it and try again the obvious question that comes to our mind at this point is how to remove a migration that is already applied to the database let's understand that with an example first from our employee model class let's remove this property and then add a new migration notice add photo path column migration is added let's apply it to the database by executing update database command the migration is applied to the database we can confirm it by refreshing our table and then take a look at the columns notice we have our new column photo path here now let's add another new migration first within our employee model class let's include that some property back save our changes and then go to the package manager console use add - migration command and let's call this migration add some property migration added let's apply this migration also to the database by executing update database command migration applied so at this point all the migrations that we have in this migrations folder are applied to the database so when we execute remove migration command from the package manager console window we get an error there we go the migration has already been applied to the database revert it and try again to remove a migration that is already applied to the database we have to first revert it to revert the migration we use update database command now if we refresh the employees table with an sequel server object Explorer vendor notice we have both the columns photo path and some property that were added by the last two migrations that we applied we have another table here with name underscore underscore EF migration history table let's view the data that we have in this table this table is created by an D framework or when we executed our first migration it is used to keep track of all the migrations that are applied to the database as you can see there is an entry for every migration that we applied now if we take a look at our employees table note we misspelled for the path column and we don't need this some property column basically we want to undo the changes that these two last migrations made to our database schema we want our database to be in the same state as it was in when we executed this migration alter employees see data and to achieve that we use update database command and then specify this migration name alter employees see data there we go the command completed successfully so the changes that these two migrations made to our database schema are reverted to confirm that let's reload the data that we have in this underscore underscore year of migration history table notice we don't have the entries for the last two migrations anymore now when we refresh the columns within our employees table notice both the columns photo path and some property are gone at this point we can remove these two migrations add photo path column and add some property to remove both of these migrations we have to execute remove migration command twice the first time we execute remove migration command it's going to remove the latest migration that is not yet applied in our case this add some property migration let's execute remove migration one more time there we go the other migration add photo path column is also removed now in our employee model class let's correct the photo path property name and we don't need this some property so let's delete that and then add a new migration let's name this migration add photo path column now let's execute the migration using update database command there we go the migration is applied in our next video we'll discuss how to upload employee photo and then store the photo part in this photo path column of our employees table we use migrations to keep domain models and database schema in sync to add a new migration use add migration command to update the database with the latest migration use update database command to remove the latest migration that is not yet applied to the database use remove migration command underscore underscore a of migration history table is used to keep track of the migrations that are applied to the database model snapshot dot CS file contains the snapshot of the current model and is used to determine what has changed when adding the next migration to remove the migration that is already applied to the database first use the update database command to undo the database changes applied by the migration next use remove migration command to remove the migration code file that's it in this video thank you for listening you [Music]
Info
Channel: kudvenkat
Views: 96,318
Rating: undefined out of 5
Keywords: entity framework core model snapshot, entity framework core add migration, entity framework core remove migration, entity framework core __migrationhistory table, entity framework core undo migration, entity framework core rollback migration, entity framework core code first revert migration, entity framework core revert migration, entity framework core remove migration from database, .net core entity framework remove migration, ef core remove migration from database
Id: MhvOKHUWgiY
Channel Id: undefined
Length: 14min 14sec (854 seconds)
Published: Tue May 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.