ASP.NET Core 6 Web API Using Entity Framework Core 6 Database First Approach | REST API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends my name is praveen today i am going  to create a web api using database first approach   if you are interested in a code first approach  then i have already created another video on   that you can find the link in the description box  below in the database first approach a database   will be created first and then using web api and  entity framework core i'll be connecting that to   the database and reading the values from the  database i'll also be using some post methods   to update the database through the web api now  in for this tutorial i am using visual studio   2022 dot net core 6 and entity framework core 6  i'm also using uh sql server express so all these   things are free you can get the visual studio  2022 community edition from microsoft's website   uh similarly microsoft visual studio express  edition is also available on microsoft's website   and if you want to download the management  studio that also you can download for free   from the microsoft website all these links i  will provide in the description box below so uh   let's uh start this uh tutorial uh so you have  to create you have to open this visual studio   2022 2022 like this and then uh create a  new project here select asp.net core api   now give a name uh for your project so for this  uh tutorial i will uh give a name like uh student   api so i'll create a student table and uh so  that's why i'm calling it as a restaurant api   it will retrieve the information about the  students uh now leave this as default.net 6   is the default one for 2022 edition so  leave it and then click on the create button so your api has been created now uh you can  delete this weather forecast which is which   is the default one uh so it is not needed  uh and there is a controller here so this   can be deleted uh so let's create  a models folder which will be   needed as we will be storing all our  model related data in the models folder and then let's go to the database uh so open  uh microsoft uh sql server management studio   and then i assume that you have already installed  this sql space so connect to your instance   now you can see this folder databases  so let's create a new database let's name the database as student and  click on ok database has been created now so let's create a new table okay so get id as integer and then here you can make it as identity so there is entered  specification so this entity you can select it as   yes so it will increment by one so when we  add something so it will be automatically   incremented and i want to make it as a primary  key so right click and then set primary key then restaurant will be named and then we can give something called as a roll number and the city in which the student  lives or let's say the country here we can increment to g let's find country let's make it 20 25 so i think uh that's it uh so i don't know how  to make it too big uh just want to keep it simple   so name roll number country roll number  is a identification which will show uh   the role number of the students so every student  will have a unique role number name is the name   of the student country the country to which the  student belongs to so that country so all this uh   can be nullable uh but it makes sense  to make this and this non-nullable so   i'm i'm unchecking that uh allowance box  now we can actually save it as for the uh so names let me give same student help our student detail table is now uh created  let's refresh this so this should come up here   it is coming up here showing detail  now let's insert some data so   right click and edit so the name of the  student say john roll number three entry uh the next one is uh rosie x entry will see the id is getting  incremented automatically i am not   adding it adding this because it is identity  column and the next student name will be okay so now these three students  have been added we can now close this   and let's go back to our visual studio now here   uh we have to add some nuget packages so  let's go to manage nuget packages browse and then search for entity  framework sql server install this okay and the next one is entity  framework core dot tools install this and then we need to configuration values so   select configuration and install  particular locate package so once these things are done now our uh nuget  packages has been added so what we will do now   is we have to run a scaffolding command so go  to your package manager console and type this   command scaffold db context uh your database  name threshold connection and here microsoft dot in and put directory models folder so now the scaffolding is done so you can see uh two uh model objects has been  created one is to context another is uh student   details in detail right now you can see this  uh error here so what we need to do is uh   create a connection string so copy this go to app settings here  create a new connection string db1 so this should work the connection string has been created so  it goes to sql server and database student so once this is created the next step is  to update the program.cs file before that   you can go back here and delete this is not needed and now windows yeah now go to program.cs here uh you need to add the namespace so use this now space what else and then you also need to add entity framework four okay so now uh below this add controllers uh we  need to add another line of code so it will be dot text here the context object  will be showing context yes paste it here within bracket since sql server and within this we have to read this value from  the uh configuration file so we'll use builder   dot configuration dot get connection string so  it'll read it from the app settings json file   and here you need to keep that key which is uh bb  con and then close this statement with semicolon   so this line of code will read the  connection string from the app settings.json   so once this is done we should be able to build  the solution without any errors so build succeeded   which is all good now next step is to add the api  controller so that we will do use scaffolding so   right click add new scaffolded item and  here select api and then select using   entity framework okay and click on add now  here select when detail as the model class   and here data context name should be  student context now this controller name   will be created uh this controller will  be created with this name click on add sometimes you may get error when you are doing  this so don't worry if you get that you just   repeat the process which means click on the  right click on the right controllers again   and uh again add a new scaffolded item and  it should be able to add the controller   so the controller has been created now uh   so that is the path so api slash controller name  and you should be able to hit this controller   uh so that's our web api is now ready  and it is ready to run so to test this   api so let's just quickly go through what it has  created so you click on this drop down and you can   see all these operations uh which are added to  this api controller so you can delete a student   you can get student detail using id or you can get  string details of all the students in the database   table then you can post you can update the student  information so like this you have different   operations which have been added to the controller  and you can go through the code and see how this   all you know the detail of that code what it is  doing exactly in the code so let's now run it when you run uh it uses swagger to run this  to test this api so you can see the all the   uh operations of this api here in  the swagger window so click on get   and let's try it out in details click  on execute now it's heading the api   and you can see it has come back with some  results like john rosie sam so these are the   uh so these are the instrument names  which we have given in the database   okay so let's put a breakpoint and see  what exactly happens so here you can see   it is trying to hit this uh get student  details so if we go here and let's   repeat it right out and click on execute  so it is hitting this breakpoint here   so let me hit f10 and now the control is in the  next line so if i go the student details keep   my cursor here and click this in the results  view i am seeing that there are three records   and if you go here you can see that  this is the record the first record   and this is the second record and so on so that  is how it is getting the data from the database   uh so if i hit continue now this same result will  appear here in this window here in the swagger ui   so that's how you create this api  uh i will do one more thing is uh   i will uh i can also use postman uh to hit  this api so before doing that uh what you   can do you can just remove this swagger and  you can go here and uh type print details you can hit here let's remove this breakpoint  click on continue and you can see the name of   the strengths you may be wondering why this space  is there because i have given n care not a and   where care in the database database that's why it  is all the space is coming you can use where here   which will which will uh in that case it will not  the space will be removed and only the number of   characters that is present that will only come  back not the extra space so that's how you uh   hit the api from the browser now so now i open  postman so what i will do is i'll test the api so to get to the default method  here now click on send   and you can see the results in the below window so the next thing what i can do is i can use  a post method so for that let me copy this   data here and then select post from the drop  down go to body select raw and here select json 89 country and let's send this so a new uh record has been created with  id4 so we can go back to our get and they send so it should get back all our friends  now you can see the fourth student has been   created and it is coming back a storm so  that's how you test uh using uh pushpan   so that's all about this tutorial uh i hope  you liked and enjoyed uh this video uh please   post your comments below and hit the  like button thanks a lot for watching
Info
Channel: tutorpraveen
Views: 31,873
Rating: undefined out of 5
Keywords: .net core web api entity framework database first, asp.net core 6 web api database first, web api core 6 with entity framework database first, asp.net core, entity framework, .net core, web api, entity framework core, database first, web api crud, web api crud operations with entity framework, crud operations in asp.net core web api and entity framework code first, asp.net core web api, crud, .net 6
Id: W0_GFp1hB5Y
Channel Id: undefined
Length: 19min 14sec (1154 seconds)
Published: Sun Mar 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.