ASP.NET Core Web API CRUD Using Entity Framework Code First Approach - Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Manoj welcome back to my YouTube channel in today's video we'll see and learn how we can create a net core web API project using Entity framework and code first approach meaning we will create our models and based on our models we'll try to create a database in SQL Server all right so far I have opened a new instance of Visual Studio 2022 so create a new project and from here we'll select our.net core web API click on next let me give name Entity Framework code first app click on next let me check this https click on create so it will create a brand new asp.net core web API project for us so let's wait for a moment so you can see our project has been created and when you create a new asp.net core web API project you will see and you will get these kind of file and folder structure if you expand controllers so we'll get a API controller called weather forecast controller we have got app settings.json we have got program.cs all right and this is the default page let's close it now the first step is right click on your project managed you get packages we have to install three packages and those packages are first Entity framework code.sql server Entity framework code.tools and extensions dot configuration all right so let's install one by one so let's copy the first one go to browse paste and it will search so this is the first one make sure you will use the correct version so let me choose six point o dot o and install so this will install your 6.0.0 version I accept and let's copy the another one which is Entity Framework code.tools I think this is installed so let's browse another one tools anti-framer tools again choose the right version install I accept let's copy the third one extensions dot configuration finished configuration and 6.0.0 install done so we have done our first step now let's close it and in your app add a new folder and give it any models and let's add a first model class add class I'm giving it name users you can give any meaningful name which will suit you so our model has been created so let's add some properties so the first property would be ID and also decorate with the key because that will be our Auto incremented column in the back end for that we have to add one namespace which is system dot componentmodel dot data annotations now let's add another property so to create a property we have a shorthand just type prop and then hit Tab Key so it will create your complete property then you can change your data type and the property name so I'm giving name let me copy this and let's change it to contact number all right for the time being I'm only using three properties all right let's save now in the models folder add another class and this class we will use for rdb context so let me give it a name user context add and this user context class will inherit a DB context so DB context for that we have to inherit a namespace which is Microsoft dot entry framework core so just hit enter it will automatically add see here the namespace has been added now inside this user context we have to create the Constructor to create a Constructor again we have a short end just type c t o r and hit tab it will create a Constructor you see now inside this parameter we have to include rdb context options so this one and give it a name like options and this will inherit our base options class all right now The Constructor part is done now we have to add a DB set entity for our users class so first let's add specifier like public now DB set inside this we have to give our class which is this users so users and users get set now if you see over here it says non nullable properties this must contain a non null value and existing Constructor all right and if you see users model you can see a green line you can see it's a kind of warning all right so how we can remove it for that right click on your project go to properties and in the properties so scroll down you see here in the label that is the property and as of now it is enable so you can make it disable from here so disable and now just save the project close it now if you go back to your model see that warning has gone if you see your context class that warning has gone from here also all right so we are done with our models part now the next thing is go to your app.settings.json we have to create a connection string so simply type connection string hit enter and here we have to give our connection sync name so the first one is let me give it name dbcon and here we have to pass our details so the first property is server and open your SQL Server click on connect database engine so this is your server name so you can copy your own server name paste it over here now next is database so which database you want to create so let me give NTT framework core DB or let's say underscore DB so database is done now third is if you notice Let Me Again open this I'm using Windows authentication as the authentication Method All right so we don't need to pass username and password but we have to give Integrated Security true so we are done with our app settings dot Json we have given our details of our SQL Server now the next step is open your program dot CS we have to register our user context inside our program.cs so here Builder dot services dot add DB context and here we have to pass our user context class so simply type user context so for that we have to import our namespace because this class is in models folder all right as you can see the important namespace and now we have to give our we have to use our in connection string so for that we have to use x dot use SQL Server and inside this we have to give our connection sync so Builder Dot configuration dot get connection string and inside this we have to give our connection string name which you will get from here so this is your connection sync name so copy and paste it over here done so we are done with our injection of our user context class in program.cs all right so let's build this project first let's see if there is an error or not so build succeeded all right now go to tools new get package manager package manager console and here let's run our migration so add hyphen migration and let me give the name as init if you see when this migration will run successfully so here we'll get a migration folder so let's run this command first so build had started build succeeded and it will give us the complete string or you can say the script of a database the operation is successful now you see here in this solution Explorer we have got a migration folder let's expand it so we have got two files so let's open this see we have got a complete script of our based on our model so name is users and we have three properties ID name contact number all right and also let's open this user context model snapshot so here you see we have ID property contact property name and this ID is also a key and the table is use us all right so let's close both of them now again open Package manager console now we have to run a command to create a database so for that update hyphen database before that let's go to SQL server and open databases and see there is no database with this name Entity framework core hyphen DB all right so you see in this list we don't have any database with the name Entity framework core all right now go back to visual studio and execute this command so update database enter so build has started it will execute all the script in this package manager console you see the script is being executed done now go back to SQL server and let's refresh this database refresh so now you see we have got our database has Entity framework core underscore DB all right you see the name is identical now if I expand this we'll see a user stable inside this tables folder with all the columns as we gave property in the user's model so as you can see we have got a table let's expand it and in the columns we'll see all three Fields ID name contact number let's also try to run it so new query select star from users and make sure you select the right database Entity framework core DB and execute this perfect ID name contact number all right so now let's go back to visual studio and here in this controller let's add a new controller so right click on this folder add controller select API and API controller empty click on ADD change the name as users controller add so here this is our newly added controller so now let's add our user context first so private read only user context and create an object so user context and now let's add a Constructor to initialize this user context class so ctor and inside this we have to use our user context as parameter so save now let's create our first API as HTTP get so HTTP get let's also give a route get users and public list users get users and here return user context dot users dot to list save so this is our get users and now let's also add a put API for to add a new user so HTTP post and let's give a route add user public tring add user and here we have to pass our users class as we will take the name and contact number property from the front end whether it is react.js or your jQuery or your angular all right so string response is equal to string dot empty and return let's say user added and here user context Dot users dot add and inside the parameter we have to pass this user's class object and after that user context dot save changes save so we have created two apis one to get all the users second to add a news all right so let's try to see how these apis will work so let's build the project and we'll run it and in 6.0 version the Swagger is already added so you don't need to add the additional packages to use the Swagger feature so let's run it and we'll test both the apis from Swagger and the Swagger is loading yeah so we have two apis one the guard second one is post so first let's add a new user so try it out and we don't need to pass this ID to let me remove it and let me add my name let's say contact number one zero one zero one zero one zero and now click on execute so it will see and return a now response you see user added we got this response and the status code is 200 all right let's go to SQL server and execute this so we have got one record as Manus deshwal and this contact number perfect now let's open this get users and try it out click on execute we'll see the single record over here as a response perfect we have got our response nice so both apis are working so let's stop the solution and let's add another get API to get a single record so HTTP get and our route should be get user and here public users get user and here we have to pass any ID so int ID return so user context dot users dot where we have to use a Lambda expression so x dot ID visually is equal to ID Dot first or default save so our single user API is ready save now let's try to create output API so the HTTP verb should be HTTP put and let's give a route update user and here let's add API methods public string update user and this will take a user's class as input so user and here so user context dot entry and here we have to pass this object dot state is equal to entitystate dot modified because we are trying to update the same entity and here user context dot save changes and return user updated save so output API is also ready to update an existing record now last one is delete so HTTP delete that is the HTTP verb and let's give a route so route delete user and public string delete user users user and here user context dot remove and pass user as object dot users dot remove and user context dot save changes and finally return user deleted save so we have done all the apis get all users get single user add a new user update existing user and also delete an existing user all right let's save and try to build will succeeded let's run the project so Swagger is loaded and we can see all the apis so let's try to add one more object and in the name let me give Virat Kohli and execute so this will add a new record in our database with the name Virat Kohli so see user added now try to get all the users and we will see an array with two users perfect this is one this is two perfect get all is working add is working now let's try to find users so get user try it out and here let me pass id1 which is manoj's ID so you see I have got a record with the details like Manoj deshwal and if I try to pass id2 execute so you see I'm getting the record with the name Virat Kohli perfect now that is also working now try to update a user try it out so let's say in my database I have two records all right so let me change uh I want to update record of this first row with the id1 so I have to pass ID as 1 and let me change my name I only want to give my first name and I want to change my phone number click on execute user updated perfect now execute this you see my name enter contact number both are replaced with this value perfect so our update is also working now let's try to execute our delete API oh okay I think I have to change something in delete actually we don't need to pass this complete user class we can pass int ID and here we can create a user's class object so users user is equal to user context dot users dot where again Lambda expression x dot x dot ID is equal equal ID DOT first or default perfect so now what it will do it will check if there is any user in our database or not with this ID or not let's also make a check if user is not equal null then we'll perform our deletion operation if there is no user we can simply return no user found save I hope you understand this this is very straightforward let's run the project again and now we'll pass only ID of user to delete it all right so open delete and try it out let's say if I pass 5 which is not present in our table you see there is no user with the id5 but still if I try to execute it says no user found all right now let me give ID 2 which belongs to this user Virat Kohli and if I try to execute you see user deleted and let's refresh this command see that brought has been deleted perfect so this is how you can create your all apis using dotted core project in Entity framework and code first approach all right now if you want me to create a react.js project and consume all those apis like we can say employee management system so please comment in the comment box or I have given my WhatsApp number so you can contact me on Whatsapp number if you have any kind of requirement with this so this is about today's video I hope you like it if you did so hit the like button share comment and if you haven't subscribed my Channel Please Subscribe I need your help and support so I'll tune the next one thanks for watching take care bye
Info
Channel: Manoj Deshwal
Views: 12,506
Rating: undefined out of 5
Keywords: .net 6 api, .net 6 web api, C# api, api c#, asp net core web api, asp.net 6 api, asp.net 6 web api, asp.net api, asp.net api crud, asp.net api tutorial, asp.net core api, asp.net core api crud, asp.net core api entity framework, asp.net core web api, aspnet core web api, dotnet core web api, dotnet core web api tutorial, entity framework, entity framework c#, rest api asp.net core, rest api c#, rest api crud operations, Entity framework core crud, EF core crud
Id: UdTAyjVGkg4
Channel Id: undefined
Length: 27min 1sec (1621 seconds)
Published: Fri Mar 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.