ASP.NET Core - Scaffolding with Entity Framework Core (Database first approach)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys computerx here and today we're gonna go with the database first approach we're gonna use entity framework uh we're gonna use the scaffold method the scaffold command and it's gonna help us transform our database tables or our entities into modules that we can use in our project so first let's go ahead and pick the asp.net core web application i'm going to call it tutorial let's create that i'm gonna pick api okay now since we're going with the database first approach let's open microsoft sql server management and create a new database i'm gonna call it person database let's expand that and pick tables and create a new table of course we have our id which is of type integer we have our first name i'm gonna go with virtual40 we also have last name with the same data type and for the sake of this example i'm gonna go with address although in real life examples you would create a separate table for the address since one person can have many addresses and an address is consisted of many things like a city a street etc now let's save that control s i'm going to call it person now that we have first let's set that as a primary key now that we have our table since we're using the database first approach we're gonna generate the code the models from the database now to do that we're gonna use entity framework uh first thing we need to go to tools and to connect up to our database so go to connect to a database in server name let's go to express refresh okay now it's done yes now you have to choose your appropriate server name for me is this one and now we need to choose our database so the person database that we just created let's test the connection everything's good okay now here in the server explorer we need to now the next step is to get our connection string so to do that we need to go to the server explorer that's our connection that we just made right now uh if you right click on this and press on properties you can see that we got our connection string right here under the connection tab so let's pick this let's copy that and we need to add it to our app settings.json so let's come here and add our connection screen now i'm going to call it person database and let's paste our connection string here okay now the next step would be to go to tools go to nougat package manager because we need to install some packages that we're gonna need to do that scaffolding i was talking about so first we need to download microsoft.entityframe server let's pick our project and install okay accept we also need to install the microsoft.entity framework dot tools package so let's install that okay now the next step would be to execute the command so let's go to tools nuget package manager package manager console and the command that we're going to be using is we're going to be using this command here to scaffold i'm gonna leave it down in the description let's copy that and let's open our package manager console again to open that you can go to tools nuget package manager package manager console gonna paste that now here we're gonna have to add our connection string which is this one if we have a double quotation here we need a double double backslash we need to remove one because if not it's gonna generate an error and here before db context we need to name it whatever we want i'm gonna name it person and what this does actually it's it creates the annotations for our properties in our model class so if we have a primary key for example it would say id i'm gonna show you how and what i mean but first let's execute this and as you can see it generated this folder right here for us repository it has the model now if you had more tables than this one it would have generated everything but since we only have one table it generated this model and this is the annotation i was talking about column click on last name key it showed the data type varchar 40 string length 40 and yeah it said it's a primary key so those are the annotations and they are here to help you that's why i like to add them and it also generated this one to talk to the database we don't have to worry about this because the scaffolding did that for us and that's how you actually that's how i personally use the database first approach i create my database all detail all the tables all the relationships between those tables and i use entity framework i use the scaffold command to generate my model so i can use them in my project thank you for watching please leave a like if it helped you and if you have any problem please let me know down in the comments
Info
Channel: Computerix
Views: 41,257
Rating: undefined out of 5
Keywords:
Id: SnU4Ulee_NI
Channel Id: undefined
Length: 7min 45sec (465 seconds)
Published: Sat Sep 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.