Entity Framework Core Migrations with ASP.NET Core in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys i'm dave and today i'm going to show you how to use any framework core migrations in an asp.net core web application to create a new database but also update and manage your database schema let's take a look okay first let's get some prerequisites out of the way for those of you that are new to the channel make sure to check out the link i'll put on the screen somewhere that'll take you to a video that will describe to you the project that we're currently looking at secondly we're going to need three nuget packages in our data access layer which is a net core 3.1 class library we're going to need the microsoft nad framework core nuget package and then i'm using sql server for my database provider you can use possibly something different i don't know the steps ultimately should be the same but there's never guarantee with a different provider if it has everything necessary to do this then thirdly we have the microsoft nad framework core tools library which provides basically the command line support for either the net cli or the built-in package manager console commands like add migration update database and things of that nature so ultimately the goal here is we want our data access layer to be self-contained we want that to be the project that references entity framework we don't want to leak nad framework references up to the other layers of the application other than maybe implicitly and we'll talk about that here in a second and we want all of our migrations to be contained within that assembly so the first thing we'll look at is our data layer itself so right now our goal is to create a new database from scratch with no schema that's going to be our baseline migration that we're going to have so for that purpose i have data context that derives from db context we have our basic constructor that takes the db context options and passes that down to the base class constructor and for now since our goal is to create a migration that would produce a database with no schema i have all of the db sets in this project currently commented out when you run a add migration command it actually looks for your data context in your project and it will look at the db sets and it will use those and the corresponding metadata to produce migrations so by commenting those out basically it thinks that we don't have any type of database schema even though in the project i still have the corresponding data entities role user and user role in the project so just a little bit of 4-1-1 there now the second thing we're going to look at before we actually take steps to produce this is the web application itself this is an asp.net core web application and you'll notice we have a project reference to our data access library we do not have a reference to nad framework and we want to pretty much keep it that way if we look at the data layer reference itself we can see that the other references were transitive meaning that they propagated with it up to the web project so we have our nad framework core reference as well as our sql server reference now you'll also notice that we do not have the tooling reference now to actually make this work we actually do need the tooling reference to propagate up to the web project as well and that's because when you create nav framework migrations the tooling actually relies on the startup project of your solution to get the connection string information to know where it's actually going to create the database so in our app settings json file i have a connection string just to my local host we're going to create a database called fault track and integrated security is true and the key and the config here is just default you can name that pretty much whatever you want to make that work we're actually going to go to the nuget package reference in our data access layer and we're going to open the properties window now when you install this new package it has some defaults for the reference so it has included assets we're not really interested in that but what we are interested in is private assets and the fact that its value is set to all by setting that to all basically that prevents the reference from intransitive so if we remove this property value you'll notice that now the tooling reference propagated up to our asp.net core web app so we have that available now i'm going to go ahead and save that change now you'll notice there's also a yellow warning icon here i've noticed that sometimes visual studio displays that i've not found any rhyme or reason as to why there's no output or errors or anything like that as far as i can tell but what i have noticed is if you simply unload your project and reload your project everything's actually fine so now that we have our tooling reference propagated we can actually create our first migration so i'm going to hide the properties window we're going to come down to the package manager console now currently this project has the startup project actually set to multiple startup projects so i have the desktop client app set as a startup project as well as the web app as a startup project primarily though the web app is the only application in the solution right now with a app settings.json so when we run this it's actually going to resolve this project as the startup project even though we have multiple selected another option is just to simply set your project as the single project as a startup project so if you run into issues with it resolving which one to pull the connection string information from it shouldn't be a problem next we're going to actually run the add migration command to create our first migration but first we're going to change our default project from fault track.web default track.data this is the project where we actually want it to create the migrations and base create our data context from that project now when we do this we're going to type add migration i'm going to call it initial since this is this is the very first one but i'm going to tell it in the case that we have multiple startup projects i'm going to tell it that the startup project is faulttrack.web so we're placing the migrations in the data access layer but it's going to pull our connection information from the web application itself so i'm going to go ahead and run that it'll take just a moment and now you can see we have our first migration in our data access library and if we look at that the up and down is empty because there's no schema there's no work for any framework to actually do then there's going to be our snapshot and we're basically in the place that we want to be now one thing that's worthy of noting is that when you actually run the add migration command the significance of the startup project is that it actually executes the program and creates a data context instance there's some supporting documentation you can find in the ef core docs on msdn but not really too important to get into for the focus of this tutorial so now that we have our first migration we're going to go ahead and run a update database command and we're going to go ahead and let it create our database for the first time again we also need to set the startup project and this takes a little bit of time for it to create the initial database that's got to establish a database connection so if i switch over to sql management studio and i refresh my databases now we have our fault track database the only thing in here should be a migration history table now if we look inside the migrations table we have our very first migration entry already in there which is where we want to be so next we're going to add our three database tables that are part of the schema so we're going to go to our data context and we're going to uncomment our db sets so we have roles users and user roles because those are uncommented now we can add our next migration except this time we're going to call this ad migration asp.net identity model now what we can see is that it created our next migration and every time you do this it already prepends a date and time prefix in front of your migration so you can see that we have the date and time stamp initial and then we have a date and time stamp asp.net identity model so it does keep them in chronological order for you just by that matter i believe there is a way where you can override the names if you so choose to in this migration you can see that it created our create table statements or expressions rather so we're not going to fixate on that too much so now we're going to go back to our package manager console and we're going to run the update database command one more time and now it's going to upgrade our database schema from an empty database schema now with that schema that we're introducing so if we come in here and refresh our tables now you can see that we have our role our user and our user role and all of our database columns and any database indexes and things we had specified as part of the metadata are all there and that's all there is to creating databases and managing database schema with any framework migrations in an asp.net core web application obviously there's a little bit of startup involved one other thing that i will note just so i don't forget is that in the startup of the asp.net application you do need to specify in the configure services method that you are adding a db context and for the options you're going to say that at least in my case we're using sql server and we're telling it we're actually passing the connection string to it from the application configuration if you missed that step none of this will work i should have covered that a little bit earlier but that's the only other setup necessary to make all this happen so guys hopefully you found this video tutorial useful thank you if you liked it like the video consider subscribing to the channel and we'll see you next time
Info
Channel: David Anderson
Views: 38,856
Rating: undefined out of 5
Keywords: entity framework migrations, entity framework migration, entity framework core, entity framework core migrations, ef core migrations, asp.net, asp.net core, .net core, c#, entity framework, .net framework, entity framework tutorial, asp.net core tutorial, aspnet core, aspnet core tutorial, c# tutorial, sql server, mssql, database, schema, code first, nuget, entity framework nuget, visual studio, .net, dotnet, csharp, how to, .net core tutorial, code first migration, dotnet core
Id: bZ74dirFHUA
Channel Id: undefined
Length: 11min 16sec (676 seconds)
Published: Wed Oct 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.