DotNet Core 6 Web API CRUD Using Entity Framework Core 6 Code First Approach

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today i am  going to create a dot net 6   web api and uh using the code fresh strategy  i will uh connect it to a database table   and uh i will use entry framework core 6 to  create this database and then i will also invoke   this web api using swagger and also through  postman so let's get started click on create a   new project uh you'll come up with all this you'll  see the list of the templates available uh so just   type api and asp.net core api will come on top  select this click on next here let's give a name i'll give some name like invoke okay so this is my name of the api so click on  next you can give any name whatever you want uh   now here you can see the default  is dotnet 6.0 which is fine   and we can leave rest as as it is no need to  change anything and then click on create button so now the project has been created you can  see uh the structure of the project here uh   so what we can do is we can go to the controllers  folder you can see the default controller is where   we don't want to use it you can leave  it as it is or you can delete it   delete this one as well uh for our  purpose first let's create a model folder   so right click on the project and  then select this add and new folder so name it model inside the model let's add a class so uh i will name this class as crypto   click on add button and a class  named uh crypto will be added now the class has been added so let's  add some properties to this class so the first property i want  to add is public paint id so get set and then public string name and next one  is a public string public hint price so this one we can make it as nullable string  so there is a question mark which is nullable   uh now we have this model ready uh so i think i  have made a mistake here so names would be crypto   name it here also crypto uh so so so the crypto  is a model class it has three properties id name   and price so now our model is ready uh next  step is to uh add the entity uh framework uh   nougat package uh so for that just right click  your project and go to manage nuget packages   go to browse here search for entity so you can see this entity framework  code.design 6.02 version so we want to use that   so select that and click on install button so accept it close this nougat package window and uh so this should be it should be installed now uh  once it's installed uh the next step is to add   a controller okay uh so we will use scaffolding  so which is basically it's a very quick way to   create a controller so it will create all the code  required so i'll create the controller and then   we'll go through the code so for that what you  need to do is just right click your controller   and go to add a controller class  select api here and here select   the last option api controller with accents  using entity framework and hit the add button   so the model class it will ask you select uh  the model class so click on this drop down   and select crypto that is our model class uh so  controller name you can see it has created its   uh so it has just made it poes controller  which is fine we'll just leave it as it is   data context class it is not created i will create  it click on plus sign and it is suggesting to use   this name which is fine invoke web api ef context  okay so this data context class is important   because it is the it is it is what you use in  your code to interact with the actual database   so for you uh data context is your entry  point to the database so click on add button   add and it will install all the dependent uh  nougat packages so now the code has been generated   remember that you may get an error for the first  time when you try to generate this code in that   case you can just repeat the process again that  is add the the controller is already added just   give the data context name and add it and it will  again run this scaffolding auto generation code so   second time hopefully it will be successful uh so  don't worry if you get an error for the first time   so now you can see uh so cryptos controller has  been added and all this controller class uh has   been added uh if you can click on this drop down  you can see uh the methods which has been added   uh like the constructor then uh there are other  methods like delete get uh post put so all these   different uh operations has been added to our api  so that is the doorway to the api if someone wants   to call this api they can call these methods and  through this they can perform uh interactions with   this api uh once this is done uh the next step  is to uh generate the database so before that   uh we need to run some commands uh if we want  to do that so you have to go to this package   manager console so it is available in tools uh  nougat package manager package manager console so   open this and then type this command add hyphen  migration and give some name any name is fine okay so and hit the enter button it will  build it may show some warning so that is fine   so some truncation warning may come  up so you can ignore those things so now it is saying that the migration  class has been added so the next step is   update database type this  command hit enter it will build so now the table has been created so we can now  check the code and we can go to the database   table to verify whether everything is done  uh successfully or not so now you can see   so that the entry point to this application  is actually the program class uh so here uh   you see this uh line of code has been added which  is uh uh basically it is uh telling our code that   use sql server and the connection string can be  found here uh so the connected string will be   can be found in the app settings dot json so you  go here you can see this is connection string   it is saying that a local db has been used to  create the database so that is uh that is the   code then the next thing i want to show you is uh in the controller class you  can see all these operations and then uh so you can see that it has it  has it is directly interacting with uh uh   using the db context it is interacting with the  database so that is what this underscore context   is the db context so this is uh this is the  dbcontest class and it is the instance of that   uh here it is initialized uh through dependency  injection so this is initialized here context   so there will be data folder also which  is created automatically and inside this   you can see this db context so it has created  it and it has added this line of code here   so uh all these things you can also do manually  uh but through scaffolding automatically it gets   generated and saves some time now let's go to the  sql server so you for that you need to go to view   and select sql server object explorer so  once you select that this window will come up   so click here and uh here you can see uh the  databases list of databases so you can see   this database has been created okay uh so ms sql  localdb is actually uh it represents represents   your local database instance so inside this uh  you can have different databases uh so one of   our database is this it will add some random key  to it so that is fine uh don't worry about it   but inside this you can see some tables will be  created uh so this is the crypto table which is   based on our model and if you actually if  you click on the columns you can see all   the property names as columns of this table so  id name price all this has been created here so that is fine uh so so now the a basic api  web api has been created with all the required   operations and we have seen the database  database table but now the table doesn't   contain any records so what we can do is we  can run the api and through swagger we can   add some data to this table okay  so i'll show you how to do that   so build a solution will suggest it  okay run it invoke web api effect so you can see all these our methods are  appearing here cryptos get post get put delete so   if you do a get now you will not get anything  although the success but you can see the data   that is that is being returned back is  uh empty so there is no data actually   so we can add some data so to do that go to  post and click on this try it out and here   uh add some currency names for example bnb  i'll add some fixes price and then execute okay so the response is 201 which is success   and it's showing that bnb has been  added let's add some more data bitcoin execute bitcoin has been added let's add one more execute and you can see the response is returning  that this values so which is uh so this is how we   can add data to the underlying uh database table  through the api now we can retrieve it and see   whether we are able to retrieve the the correct  one or not okay so click on this get and execute and you can see in the response  body that all the three   points which we just added are appearing  here so we can go back to our visual studio   stop it then go go to the sql server here again  go to the table right click and say you data here you can see all the three items added so next step is to use postman so what  we need to do is uh write it again   because i want the url so i'm running it again you  can close this database table instance not needed so so this is this is what i wanted this uh base  address so i'm copying this base address now and so before going to postman i will do  a simple test so here type api slash cryptos slash one okay i think i made a mistake here so cryptos one okay so we are returning uh so  we are getting the data back   so which is correct now the same behavior should  happen when we use postman so let's go to pushman click on this tab enter the url hit the  send button and you can see the json   request coming back if i say three i should  see the adjacent request for the other con   dos coin so that is also fine uh you can also  post some requests so for that just copy this   go to select your post and then go to  body raw and here you should select json so we want to send the request  so that's a use the top box here   so send the request so make it  zero and then let's say type some coin name and some value let's say  number four and then hit the send button okay so i think i have made  a mistake here okay let's   do it again okay so now this coin has been  created so we can go back to our get method and this time what i will do is i'll not give  any parameters here i will just i want to see all   the data so i will not give anything here with  the send button and you can see in the bottom all the data in the database so we have four  records so all the four records uh are coming   back back as response so and the last one  this is what we just added through postman   so this is a simple simple uh uh a very  simple way of invoking the web api so i   hope you have learned something uh  from this tutorial if you have any   comments please let me know and please  subscribe to my channel thanks a lot
Info
Channel: tutorpraveen
Views: 23,621
Rating: undefined out of 5
Keywords: web api, .net 6 core, asp.net 6, asp.net core, web api crud, web api crud operations with entity framework, web api crud operation, web api crud and postman, entity framework 6, entity framework, c#, entity framework 6 code first
Id: 8e-30LJb97g
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Sun Feb 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.