Asp.Net Core Web API - CRUD operations in REST API using Entity Framework Core and SQL Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Manoj welcome back in today's video we will try to create a current operation application using dotnet core web API and in this video we will use Entity framework and with the Entity framework approach we will use code first approach means we will create our model classes and based on our models we will create our database all right and we will also use Swagger so we don't need to install Postman and to give request response body everything all right so we will use inbuilt Swagger application so let's quickly create a new project so for this video I'm using visual studio 2022 you can use any visual studio version with support.net core web API so I will create a new project and from the template we will use asp.net core web API so I have created multiple API projects so it will give me a direct access or you can say direct link so just click on it and click on next and let's give a name let's say crud operations in dot net core click on next so uncheck this configure for HTTP and click on create so it will create our brand new.net core web API project where we will see our default file and folder structure whatever we get from the dotnet framework so you can see the project has been created and you see the solution Explorer so these are the default file and folders so if I expand the controllers we get weather forecast control this is a inbuilt or you can say the default controller we have app settings.json we have program.cs and this is a default page let's close it and if I try to run it so it will open our Swagger feature on our browser so I'm using dot my Chrome browser as a default browser so it will open the content on our Chrome so as you can see on your screen so this is localhost 5140 slash Swagger index.html so this is our base URL and as we enabled the Swagger which is inbuilt feature of dot net core web API 6.0 so by default we'll get this weather forecast get API if you expand it click on this try it out and execute so it will give us some default values all right so let's go back to little Studio and open this weather forecast controller so here we have an action method if you see this weather forecast and we have this get Method All right so based on this logic it is giving us some data you can see this is an complete object and let's open the properties so we have launch settings.json and this file is responsible which feature or which controller which method you want to render on our initial load as you can see the launch URL Swagger and this is our base URL as I already shown you if I again go back to browser this one this is our port 51405140 so let's close both of them and now stop the project first of all let's add a folder where we will keep our classes so give it a name models and inside this model let's add one more class and let's open [Music] yeah so here I'm using a name as brand so inside this brand we'll have some properties so to create a property just type prop and hit tab it will create your property details so you don't need to write a complete line again prop and this will be a string type and I'm giving name and again prop string let's say category and prop is active save and let's make the label so if we don't pass no need to worry it will automatically put a null for this particular property save now right click on your project and now we need to add some packages so manage new get packages first one is Microsoft Entity framework core so we need to install this it is searching and make sure we'll install the latest and stable version so the first one click on it and you see the latest in stable version 7.0.1 click on install so it will install this package click on accept ah let us wait for some time so it will be installed yeah and the second package is microsoft.framework.co.tools so let's copy go back to browser paste install it accept and the last package is framework code.sql server now let's try to install that SQL Server package install by accept yeah that is installed so now we have installed all our packages SQL Server tools and asp.net core Entity Framework let's close this now inside our models folder we'll create our DB context class so right click on this model click on ADD class and give it a name like brand context you can give any meaningful name which will suit you and we will inherit this with DB context class for that we need to add one namespace which is Microsoft entity framework.com so you can simply hit Ctrl dot it will display all the namespaces whatever is there in this project so inside this we need to create a Constructor to create a Constructor you simply type c t o r and then hit tab it will create your Constructor you can see the class name and then this method name is both are same this is not a method this is a Constructor and here we will use our DB context options class so DB context options and here we need to pass our class name which is brand context and let me give it a name like object options and this will be inherited base options now we need to create a DB set or offer of our brand context so DB set brand okay let's make it public so we are done with the rtb context class so we just added one Constructor and we passed DB context options and in that we pass our brand context all right so now next step is open your app settings dot Json here we need to add our connection string so simply type connection string and in that object we need to pass our connection string name so I'm using brand CS you can use any meaningful name and inside this the first property is server that we will copy from our SQL Server let's open SQL also and the second property is database so again it's up to you you can give any meaningful name I am giving brand DB and I will use Windows authentication so no need to give any username and password as you can see Windows Authentication so let's copy the server name and click on connect so here keep this server name and now we need to pass Integrated Security true and we also give multiple results set property as true so multiple result sets actually this is multiple active result sets equal to save so our connection string is ready now we have created our context class so in asp.net core we have built with dependency injection so now we need to register our database context in the built-in ioc container so let's open our program.cs file and inside this you will give our brand context so here simply use Builder object build a DOT services Dot add DB context then in this context we will pass our brand context class so brand context and use any Alias so options and here we will pass options dot use SQL server and inside this create any name so I'm using Builder Dot configuration dot get connection string get connection string and inside this connection thing we need to note here we need to give our connection string name which you will get from app settings dot Json so this one so copy this name copy and paste it here save so our database context registration is ready let's try to build this so build has started yeah build succeeded so here we will create the database using the Entity framework core migration feature so let's click on this tools from the file menu and you get package manager package manager console so let me maximize it little bit so here we need to give a command add migration space initial so this command generates code to create the initial database schema which will be based on the model whatever we gave in our brand context class so we'll started build succeeded so now if you again go back to solution Explorer so you see we have got one new migration folder so if you open your migration let me close this window see so it will create a Brands table see this is the script which will be responsible to create a table in our database all right so it will create brand with these fields ID name category is active all right so this is what we got when we run the add migration class again open your package manager console so now we need to give a command like update database hit enter so it will generate some dummy data in our table all right so build as you can see I think something is wrong with this by mistake save let's run this command again so as you can see a connection was successfully established with the server but then an error occurred during the login process so I intentionally wanted to show you this error because this is a common error so for that we need to pass one property which is trust server certificate is equal to true so now save this and again let's run the same command so update database so now this will run successfully we can see the database is already up to date perfect now let's open our SQL server and refresh our databases let's open it so we hopefully see our database and the database name is brand DB okay so it will take some moment perfect you can see the brand name is there let me open this one I hope we will see the table as well brands and also we'll get one more table with this migration so it will take some time perfect so this is our table let me open the data perfect as you can see ID name category is active all fields are there so our database is ready now let's go back to our project and let's add one more controller so add controller and click on API and API empty add let me give it a name brand controller click on ADD perfect now we need to add our DB context class over here so inside this class private read only brand context DB context and let's create a Constructor so again ctor head tab and inside this Constructor so underscore underscore DB context is equal to DB context perfect save now we will create our grid operations like HTTP get HTTP post put delete all right so the first one is hashtp guest as the intelligence is suggesting so hit tab so public async task and action result and this will be a list so I'll use I enumerable and inside this we will use our brand class so brand and let's say give it a name get brands and if underscore DB context dot Brands equal equal null means if there is no record in our database of this Brand's class then we'll return no not found perfect otherwise we'll return await underscore DB context Dot Brands dot to list missing done so we are done with our cat method save now let's try to add another method for get by ID for a particular movie so here we need to change something and first of all get brand let's regret brand only and in the parameter we'll pass ID So based on this ID we will fetch our brand from the database so the first is if there is no brand will return not found otherwise we'll return a brand so let's say where brand is equal to underscore DB context dot Brands dot find a sync and here we'll pass our ID now we'll check this brand if this brand is equal null then again we'll return not found else will it turn this brand this is not a numberable so we need to delete this because this is a single brand save so we are done with our particular record with a single brand perfect so both get and get by ID both are ready now we will try to add our post method so here HTTP post and we'll add again public a sink task action result brand post brand and this will take our brand class object so brand and here DB context Dot Brands dot add the object of our brand class which we are getting from the front end it could be your react.js it could be your angularjs or it could be your MVC and await DB TV context dot save changes async and now we will return our single movie all right so return created at action name of so it will pass our get brand method which we already created for a particular you know this brand so here ID is equal to brand dot ID and brand so okay perfect so what it will do this request you will pass from the front end based on that this it will go to our DB context class and through the logic of this add method it will save the brand details in our database and after that it will return the same brand to the front end all right with the ID save so our post method is also ready so let's try to run this first so add and get we will test so Ctrl shift B just try to build it just in just to check if any error is there or not perfect now run it by mistake here we need to pass a object of ID so in the get we will pass ID so now rebuild and run perfect get post get so let's try to add any brand so click on try it out so this is our request so we don't need to pass any ID so I'm giving let's say name Apple iPhone and categories let's say mobile phones and his active is one click on execute so now it is taking time to add the data in our database so just wait for a moment perfect you can see two zero one ID this Apple iPhone mobile perfect now if I go back to SQL server and if I execute this script you see name Apple iPhone category Mobile iPhone one perfect so our post request is working as expected let's quickly add one more Samsung phone execute done so this is our individual request which we get by the response so again execute the same command so we have two records in our database perfect so now try to test this one this is to return all the data from our table so just click on try it out click on execute so you see we have an array of two objects one for apple one for Samsung perfect now let's collapse this open now get my ID try it out so if I pass id1 execute perfect it is giving the first object with the name Apple iPhone if I change the ID and if I execute perfect it is giving the number 2 which is Samsung iPhone if you see here also this is the second row perfect so all apis running let me close this go back to visual studio again and let me do control moh it will auto collapse all your functions so now we'll try to create the put API which is responsible to update a particular or existing brand all right so the HTTP verb should be HTTP put so again public async task action result and let's say the name is API put brand so here we have two things first we need to pass the ID of a particular brand which we need to update and second the details whatever we want to make change in the database all right so the first parameter ID and second one is brand brand okay now here if this ID whatever you are passing from the database is not equal brand dot ID there will say return but request both should be identical and if this is not the case then underscore DB context then we'll set our state of a particular entity is modified so here we'll pass our brand object dot state is equal to entity state DOT modified means we are going to update our database the particular record now let's use try catch await underscore DB context Dot save changes and in the catch block we'll use DB update concurrency exception and in our case if not here let's create a new method just to check our brand whether it is available in the database or not so let's make it private brand available and it will take ID and it will return underscore DB context Dot brands dot any x dot ID equal equal ID not get value or default so basically this will tell us that whatever ID we are passing is available in our database or not okay so now we will use this one here so if is not brand available with the ID whatever we are passing from the front end I am talking about this ID then return not found else true same exception and after this sketch return okay means our operation is successful so let's save this now so we have created our get all get by ID we have created our post we have created output so now the last API method which is pending is delete all right so HTTP delete so here again we need to pass the same ID in the parameter so let's copy this from here and paste it and again public async task action result and let's give it a name like delete brand and it will take ID parameter from the front end again if underscore DB DB context Dot brands equal equal null then return not found and if this is not the case then we'll try to find the particular brand so where brand is equal to underscore DB context Dot Brands Dot find a sync and pass the ID again check this brand which we are getting from this command so if brand is not equal null if equal equal null then return not found else underscore DB context dot Brands dot remove pass this brand here DB context dot save changes and finally return okay save so we are done with our delete method also so all our apis are done so let's build the project again and see if any error exception is there perfect no failed everything is up to date just start our application and we'll try our put and brand this delete both the apis on the Swagger so our Swagger has been loaded but before testing this put and delete API I want to show you something so I'm trying or I'm planning to create a complete full stack project using.net core API Swagger code first approach and the front end will be in react so this is the application it's a kind of e-commerce portal so it will have each and everything it will have a dynamic menu from the admin panel you can create your categories and then subcategories and then products you can also add Distributors and then we can also assign permissions like this a distributor will have this much excess of let's say there are 100 categories but you want to assign a distributor one only ten so you can do that from your admin panel and we'll also have a reporting in that project so dealer wise you can say category subcategory product and you can also export the same report in Excel format and we will have a provision to change the status of your payments now we have admin module and we also have distributor module so they will login from their panel and they can view all the products they can check the quantity they can see the image price and they can add products in the cart and they can place their order and will also try to create one dummy payment Gateway in our project and the third module is a front-end e-commerce portal for end users so whatever product admin or distributor will add they will be visible on our fronted eCommerce portal and anyone can go there and register themselves on our portal and they can search categories of category all the products they can add and they can place the order all right so I'm planning to create this complete full stack application but I want your opinion all right whether you want me to create this or not so I request all of you who are watching this video so please give your comment in the comment box all right now let's go back to our browser so here so let's open this put API so in our database we have to record so let's try to update the first one well it's the second one so try it out and give id2 because we have two ID of Samsung phone so here I'm changing it Samsung curved monitor and category is let's say monitors or let's say gaming monitor and active one click on execute so it says bad request we forgot to pass the same ID over here so we need to pass this also execute as you can see we got 200 access which means the success let's go back to visual SQL server and execute so you can see the details has been changed Samsung monitor gaming monitor so output API is also working perfect now let's try to test our delete API so let me give let's say id2 once again I'm executing this so we have two rows perfect execute 200 done that second row with the id2 has been deleted perfect so all our apis are working so this is about today's video but I have one request or you can say I have one announcement so if you are working on your final year project uh or you are working in a company uh in a internship based or in a regular based and you are facing any problem regarding.net rex.js SQL Server jQuery ajax HTML CSS all right so I do these kind of paid services so if you're looking for this kind of you know uh services so please contact me you can contact me on my Instagram my Instagram handle is open programmer and you can also find my email on my channel description so this is about today's video I hope you like this video if you did so hit the like button share comment and I'll see you in the next one thanks for watching take care bye
Info
Channel: Manoj Deshwal
Views: 35,896
Rating: undefined out of 5
Keywords: CRUD Operations in asp.net core, CRUD Operations in asp.net core web api, CRUD Operations in .net core, CRUD Operations in .net core web api, CRUD Operations in rest api, CRUD Operations using rest api, CRUD Operations REST API, Asp.Net Core Web API CRUD Operations, How to build Web API with Asp.Net Core, Asp.Net Core Web API CRUD with Entity Framework Core and SQL Server, .Net Core 6 EF Core CRUD using Code First, Insert Update and Delete in .Net Core Web API
Id: Fp6s89A136Q
Channel Id: undefined
Length: 39min 53sec (2393 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.