CRUD Operations Using Database First Approach In Entity Framework Core 6 and ASP.NET Core MVC 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is praveen and today i am  going to teach you how to use asp.net core mvc   and connect to an existing database and read  the data from this database or update delete   or create new data in this database  so that is the purpose of this video   and i am using entity framework core 6 visual  studio 2022 for this demo so if you don't have it   in your system then please download it you can  find the links in the description of this video   before we get started also remember that i will  be using sql server so you also need to get   sql server express and sql server management  management studio so these are the two software   which you should download if you want to run this  or create this application in your system so all   these links will be in the description of this  video so let's get started so create a new project   using visual studio 2022 go to uh the  search here and search for asp.net mvc so we i'm using mvc here model  view controller so select this   and get the next button here give a name so let me give some name here uh i'll  say country app because i'll be using a   database table which will contain a list of  countries so i'm just giving this name country app   so next uh i'll be using dotnet 6.0 so this is  fine the default one click create the next step   is actually to create the database okay which to  save time i have actually created a database but   i will let you know what to do here uh so if you  have uh already installed microsoft sql server   management studio then uh open it go to file  connect and then connect to your local xql express   server so that is the one which is created when  you install sql express so and then hit connect   and then it will open up in your left pin like  this so expand it so here you can see a database   called as human resource which i have created  so in this for simplicity i have just one table   called as country so you can create this database  to create it just right click new database   and then give the database any name and click on  ok so this database will be created and once it   is created expand it go here to tables right  click new and select table so you will get   a window like this where you can enter the column  names and all the fields in the table so let's   go to this table which i have already created so  here go to design and then you can see all these   uh columns in this table okay so here you can  see that i have a primary key which is id so to   uh so you can create this uh all these columns  and then right click this and then uh make it as   primary key so here it is coming as remove primary  key because i have already made it primary key but   otherwise uh you will have this option to create  the primary key here to make it a primary key   so do that and also this is the identity column  which means it will be created uh so the the the   values in this column will be inserted  automatically whenever i insert a new   record so no need for me to explicitly insert a  value in this particular column so for that just   select the column and here you can see this is  identity so go here and select yes so by default   it will be no so you have to select make it yes  and once you do it it will be it will become an   identity column and then you make it primary key  and then you add all other columns like this and   uh the data types as shown once it is done  you can go right click and edit the rows so   here you can see i have already added some  country names but if you edit it so automatically   you can actually this column will automatically  increment as i said before now in the name you   can actually add the country names for example i  will add one country to show you how it is done so tokyo uh population so let's say it is in  millions uh i don't know exactly what is the   population of japan but so i am just giving some  uh dummy values here uh economy so five and uh   japan's currency is uh yen and then just  select any other field and this will be updated   okay so once it is updated so we can close this so  we have all the records now in the database table   so let's come out of this and go to our dot net  project so in our dot net project we have to uh   we have to actually add some packages  nuget packages so right click manage locate packages browse and here yes so this one is one package sql server install it except finish then the other one  is tools install that also and then install one more that is called as click configuration so this one microsoft dot  extension start configuration   so that you can read the app settings install this so now we should have all the  packages so you can see that   these are the packages we have installed uh let's close this okay save close now the next step is to create uh we already  have a models folder so let's step the next   step will be to scaffold basically to create all  the models and db context uh class so for that   we have to type one uh one command uh so that  command we have to we have to go to this tools   look at package manager package  manager console we have to type it here so the command is scaffold fndb context server equal to dot slash dot is basically  dot slash means it will go to your local   you can also type your machine name slash sql  express or if it is installed if your sql space   is in another machine then you can give that  name as well so give that path now database equal to human resource let me call on trusted  connections trusted underscore connection is true and then semicolon after that microsoft dot entity framework core dot sql server hyphen so where do we want our classes to   all those objects or classes to be created  so that is our output directory so output dir   models so that is the command i hope  i have typed it correctly let's see build started build succeeded so yeah so this warning is fine uh so now it  looks like uh all the objects are created you   can see this context object uh has been created so  inside models folder you can also see the country   uh entity has been created and the  database context object has been created   okay so here it is saying uh that we  should not have this connection string   uh here in the code rather we should it should  be configurable uh so what we will do is we will   go to the app settings and we will create  it there okay so go to the app settings and   then uh type and create this uh let's let's  create this connection connection strings let's give a name db on this is the same i can copy it from here so the connection string is fine so it  is this is the additional one create it   so now the connection string is created  and we can delete this we don't actually   need this line here rather what we should do is  we should go to the program.cs and add a line here   i will add here to your builder  dot services dot add db context so the type here is human okay i have to actually copy some name spaces here using soft dot entity framework code and also what else okay country app tree dot what else now come back here and  then type human resource context and then bracket options dot user sql server and within it hit enter bring it to  the second line within this uh builder dot configuration dot get connection string and db con close it with a semicolon so now you  have this uh so we have now added the db context   uh to the builder as a middleware so which means  it knows where to go and get the connection string   and read the data from there so now we have  told the application that this is the location   so once this is done uh so you have the  connection string here we are reading it here   and we have the context class ready so  so right click on the controllers folder   add and go to new scaffolded item so here you can see mvc controller with views  using entity framework select this click on   add button model class so ours is country select  country data context class human resource context   so special things is okay click on add okay so the scaffolding is  complete and we have all these   methods inside the controller so this  controller has been created countries   controller and you can see all these action  methods added within it so the next step is   to point to this controller so that we can  run the application so for that go to views   shared layout and here add one more link  so let's copy this paste it here and say country and here we can give the name  as this controller which is countries countries actually the name can also be countries so this link will be created  now let's close it and build it we'll succeed run it okay so the home  page has opened up click on countries now you can see all the countries being  retrieved so this is the retrieval   part where you are able to this  application is able to read   all the data from our database and show  it here so let's try to edit edit the data   so click on this edit button so you can see  this page has opened up so let's make it 32. so here you can see the 32 number has been  reflected back let's see if the details is fine   for details you can see the details of that  country so which is also 32 so everything is   created properly so like this we can go and we  can change other countries also let me go to india   and here let's make it four instead of  three so now you can see india also is   four so let's now test the delete  functionality uh so let's delete uh japan okay so it is asking whether you  want to it is confirming from me   so yes delete and now japan is gone  from the list let's create a new one so thailand capital airplane let's create it now we can see island is also created here so   uh so that's the crowd operation create read  update and delete hope you like this video   today and if you want to get more such  updates then please subscribe to my channel   and hit the bell icon and do not forget to  like this video thanks a lot for watching
Info
Channel: tutorpraveen
Views: 34,304
Rating: undefined out of 5
Keywords: ef core, .net core, entity framework core, entity framework core 6, entity framework, entity framework core 6 database first, asp.net core database connection, asp.net core, asp.net core mvc database first, entity framework core crud, asp.net mvc crud operation with entity framework, asp.net mvc, crud operations in asp.net core mvc
Id: rUtWgEs07eI
Channel Id: undefined
Length: 17min 29sec (1049 seconds)
Published: Thu Feb 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.