ASP.Net Core 5 Api with EntityFramework Code First and Migrations Setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right guys so we're back again with another tutorial uh and today what i'm gonna do is i'm gonna show you guys how to create a asp.net um core application with entity framework migrations and how to set that up basically on a core application so right here i have one that i've already done already but i would like to go through the step completely with you guys so basically a lot of guys have been asking me how to do this and stuff like that there the best thing you want to do is with migrations is you don't want to make it based part on code first uh chance so you don't want code first to handle the migrations you want to basically do the migrations yourself so you want manual migrations and you want to do a connection to your to your sql server so basically this is how you do it so i'm going to start off with the new project right now um so i'm going to call core application web application uh i see that's fine so i'm going to call it maybe chest tutor let's call it citizen tutorial uh citizen asp at core code first tutorial sorry for the long name guys but there's so many projects in my um drive that it just keeps going off so i'm going to create an a api um i'm going to create a web application right now create a web api call application uh i think that's fine sp internet call uh you can do five let's do five asp.net call five hope anything is not different on that one anyway so we have a blank application here um let's pull this uh basically so i'm not gonna run this application uh you can run it uh if you want to see it so i'm gonna run it right now just so you guys can see it but i hate running.net applications like this to take a long time to make videos but i'm gonna run into the one time so you guys can see um the api and basically it's just a basic weather api with with the all of the swagger stuff and in it and stuff like that there so asp.net 5 um also comes with swagger and all of that there in it so it basically makes it much more simpler to to run things anyway cool so basically what you want to do is you want to start up your application at uh by saying you want to install certain things so you're going to go to manage and basically what i want to do let's see what's installed here already ah okay cool okay so i want to have entity framework which is the call one so microsoft entity framework call and that's five the latest table version i'm gonna install this might take a while to install this and there we go i also want microsoft entity framework sql server as you can see i have my server here and i want to connect it now i'm using windows authentication for this one you can use obviously you just change the string to whatever you're using anyway but i'm going to show you how to do that and i also want microsoft entity framework um code or tools this will allow for migrations on my project and manual migrations i might say on my project the reason you want manual migrations is you actually want to see things happening before you even run the application so and if there's any problems with the any of your changes or anything you just don't want to run the application and mess up data so it's best to see it before you even start doing everything and i like manual migrations because then you can just move it around and then you know exactly where things are and it gives you a more sense of control over your code right so now that we've installed that there i'm going to create a data model a data model folder in my data model folder i'm going to have let's say a citizen class which is going to represent one table in my database so i just want to show you guys one of course um so i already have a basically a table here so i'm just gonna copy this one there i try to do employee one and i decided ah citizen is more uh unique i haven't seen somebody doing one with citizen one before so i said ah let's try it um anyway so leaving that aside i have now a citizen model there so basically this is just basically going to be my table i want to show you how to connect this table and create this table using code first now and what you also want your in here is you want like a data connection string in here and in here so basically i've already written one that i like here the reason why i write it this way is because i like to save time a lot of time uh by doing these things already and just copying the code uh when necessary and just putting them in because it saves me a ton of time also i believe it saves everyone a bunch of time because it takes a lot of time to upload my videos and stuff anyway what i've done here is i've put a um basically a stop value into app settings json and i've named it under the conditions data and in data is connection strings because i want if i wanted multiple strings then yeah cool and default connection now i'm using persistent ground controller so it's the same url that you would use to um basically connect to your sql server on any other application um i'm just basically using a url that i have that's connected to windows authentication and basically the one that's connected here and as you can see i have no databases here right now so i'm going to show you guys how to start this code first sequence now ignore this application for now and i'm only looking at this this is an api call one all right so this one that we're talking about because i'm just pulling code from that one to show you guys what and where to to go with all right so once we got that there we already got a connection in there um what we need to do now is establish a not in the startup we have to establish services in the startup for sql server and basically it's just one line of code that you just gotta add in and that's basically this year and that's actually services that add db context the data context use sql server that's because that's coming sql server is coming from empty asp.net core and here's my data strings connection the way i the way i put it is in the json format and you know when you're getting configurations from the json app settings you gotta put a um um a colon um there between the uh inner parts of the json now i don't have a data context here so i'm gonna create a data context so basically um going to go my data model and i'm going to say add a class and we'll call a data context model so i've got my data context there and cool i think everything is going off now so let me just see what i have here my data context model is there basically there and cool now in my data context model i'm going to inherit db context and in db context i'm going to edit data context of db what is it um i think it's tv settings yep there is it there we go i'm just gonna copy that over i'm gonna look up stuff now yeah so it's dp context options into there and into the base let's make this more neater for you guys to see so you can pause at any time and then um look at the code but basically all you got to do is that and then you got your public db set of citizen that's my class and i'm calling it citizens and i'm making it get and set so now that i've set up code first i need to know that it works basically the way i want and i also want to add my custom manual migrations into it so what i'm going to do is i'm going to basically add custom migrations into this this this application right here so let's just build this application again and you notice that i'm keep saving all the time that's because i want to always if if something happens maybe the lights go off or but as a power card or some sort of thing like that there um i can basically save my data the way i needed to save my data now basically i'm not going to run this application it's just to show you how to connect everything in there um now i'm gonna do a remember when you're doing i'm gonna do the migrations now for this yes the initial migration and show you how guys doing it so right now you can still see that there's nothing happening here right so that's cool and to do migrations now you gotta select your your api at the top there make sure if using multiple projects you're selecting api you're running to multiple projects at the same time just select your api one and ensure that your package console also has your api one if you don't have a data context outside of it now what you want to do is say add migration right and say this is the initial creates of the migration so the initial creator basically create whatever data you have in there now right and let's see could not load and b ensure that it is referenced with the covers okay cool so i i've referenced the wrong thing so i'm referencing the right thing so you see now why i'm saying your default project needs to be hand-in-hand with the data context is sitting so your api at the top and then you're where your data contact is sitting in which project so cool let's do this again and i hope you guys are seeing at the bottom here let me see if i can try and bring this up for you guys yeah so there you go so i added my add dash migration all small small letters and i'm doing an initial create cool right awesome initial creator starting cool now what happens here with the initial create is basically um builds up my sql coding in the background to create my stuff so if you really want to see how it works here's how the migration works in that in that particular say and what i want to do to this now is i want to upgrade the update the database here so see if i'm actually connected will via code first now so that if i make any changes in my basic uh commands uh or my model i'm able to see it in almost instantly without running the app so i'm going to update this here and what this should do is now create a database in my database there we go so now if i open up my database i see my citizens at voila now some of you guys might have another problem where you have two data contexts now in order to solve that there all you got to do is just add an extra piece of code here which is dash context and the name of your data context it can be employer context it can be a lazy context i don't care what what you call your data context uh model right uh or your db context model but basically to find the right more context that you wanna attach it to if you have multiple in the same project this is what you would do and you'd basically say okay cool and you'd hit enter and go straight to it um i'm not gonna do that now because that's gonna be unnecessary the reason okay i'll do it for you guys and show you what i mean so test and that will create a test one now the reason the good thing about this migration is it's showing me actually if i really need this here which i don't so i can just go there and delete this this migration because i don't really need it and i can rebuild well it succeeded and now i go back to my database even year two if it still gives you errors and stuff here in this point all you got to do is say data context well mine is called electric sales can be called anything all right so there's no changes currently in there but obviously if i needed a change to be in there now what if i wanted to drop off a i wanted a drop of data but so i say save say add my migration now and say removed d o b data but cool watch this removed the database from there and now as you can see it's still it's still there but i need to upgrade my database now succeeded let's see what is now city in the citizens table cool see it works migration works now when you do this here you don't necessarily have to say update database so once you add your migration in then you run your application it'll basically do that for you so suppose now i went back here and i said um uh i wanted to add public string uh test all right and i put a get and a set and i save right adding my migration now i'm saying it's a test don't worry about it my bad i'm saying it's a test and i run my application now here's the thing let's check if this has changed hasn't right this is the reason why you have to do manual migrations because it will only change if the model has been touched for the first time if you understand what i'm what i'm going for so it'll only change if the model has been touched for the first time so if i had the model in here with like a notoriety or something and i'm getting something from it so suppose i'm getting something from my model which is basically um so i can go do this sort of thing let's say get something from here in the next episode i'll show you how to do gets and stuff like that there but basically this this episode or this tutorial is basically just telling you how to set up migrations how to set up code first in dot net entity call five thank you for joining us and we'll see you again in the next video cheers guys bye
Info
Channel: DevelopMeDev Official
Views: 4,222
Rating: undefined out of 5
Keywords: ASP.Net Core, asp.net core 5, .net core, Code First, EntityFramework, Manual Migrations, API, Visual Studio, 2020 latest, coding, tutorials, C#
Id: RuHfCBxkbeI
Channel Id: undefined
Length: 18min 30sec (1110 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.