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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back in today's session we are going to perform card operations with web API I am going to create one new project here I am selecting asp.net core web API this is my project name which I am storing in this location I am selecting the latest dotnet framework here click on create our project has been created here here session we are going to use Entity framework DB first approach for that I am adding nuget packages here database we are going to use SQL Server I am selecting the SQL Server and I'm installing latest stable version it's installed this is the database which we are going to use it in this session and I have one table called products here so we have the columns like ID product name price and quantity ID is the primary key and it is identity field which is Auto incremented here we now need to insert any value into the ID field so remaining other fields we have to insert here so all three fields are not null so we need to pass the data here first I am going to create one model here I'll create one models folder inside that I am going to create product class this is my product class here I am going to Define our properties so here we need ID product name price and quantities and we have to match the data types also these fields are mandatory fields I'll create another folder for data access I'll make it as data access layer inside this I am going to create appdb context to communicate with our database I'll make it as this is my context name and this will be inherited from DB context click on DB context go to the quick actions generate the Constructor with options here I have to add product table property to communicate with DB this is going to be DB set and I have to call product here and here we have to add the models so this is coming from Models folder and this I will make it as products so this is the products table which we have inside the crowd with baby API database so we are going to communicate with this database for that we are going to use the DB context with this we are going to communicate to our server to communicate with our SQL Server we have to add the connection string inside app settings.json here I am going to add the connection string this is the connection string this is connection string name and this is my server name and I have multiple SQL instances that's why I mentioned my SQL instance name if you are using single instance then you need to mention this instance name here so here we have to pause our database name I can copy it from my SQL Server now we have to register this connection string inside our program.cs for that I'll open the program.cs here I am going to register my connection sync for that I will take it from my previous example here we have to register our connection string with database context this is my database context name and I have to pause it here and I have to add the reference here this is coming from data access layer and my connection string name I can take it from here this is my connection string which we are passing here so we are going to use this SQL Server here to communicate with the database this my app DB context is the mediator between our application and the database here I am going to add one API controller for that right click on the controllers add controller here I need API here we have to check API here we have to select the API by default MVC controller will be selected here we have to select API and then we have to select API controller EMT click on ADD here we have to provide our controller name this is product controller I will add it here to connect with the database we have to initialize our app DB context here select the context go to the quick actions generate Constructor here now we can communicate with our product table first I will create a get action method to read all products this is going to be get here first I have to read all the products from the database so it will return the list of products so before returning the products we have to check whether we have any products inside this variable for that I will write a if condition if count is 0 then I have to return data not found and here we can return custom message also products not available so this is the custom message I am going to return and if we have the data inside the products then we will return okay and here we have to pause products so this is the action method to read all the products from the products table I'll write another action method to get the data based on the ID which is passed by the user here I will pause the product ID to read the data here I have to check products are available with the ID which is given by the user for that I'll check context dot products find with the ID if product is null here we will get whether null or we will be having the content again I will return the not found here and this time I will pass the custom message like product details not found with ID here we can pause the ID which is given by the user if it is Data is available with the ID given by the user then we have to return okay and then we have to pause this product and this is also going to be get action method and here we can check if there is any unhandled exception we can handle that also go to the snippet surround with try catch I can return as bad request here we can pause exception message same way here also we will check is there any errors so I am going to copy the same catch block so this action method is going to return all the products from the products table and this we have to pause input parameter as product ID based on the product ID we can filter the data if product is null then we are going to return this custom message otherwise we are going to return the product here we will create one action method for post that is create here we will pause our product model to insert the data this is going to be model and this will be post action method here I am going to straight away add the model 2 context save changes and finally I'll return okay here we can pause the custom message product created this is the message which I am going to return and here also I have to check is there any exceptions for that I'll keep it inside the try catch copy the sketch block here I am going to create one action method for put that is update here again we are going to pause the product model to update the data here before updating the data we have to validate the model like if model equal to null or ID is zero then I am going to validate data again I'll check if any one condition is satisfied then it comes inside this block and then I'll check if model is null I am going to return bad request here here we can pause the custom message invalid else if ID is 0 then again I am going to return this so I'll pause this message to the user if both these conditions are not satisfied then it comes here then here I am going to validate product context Dot so ID is greater than 0 then I am going to check is there any product available with the ID which is passed by the user if it is null again I have to return this message here this time it will be not found and product not found with ID here we can pause model.id else we have to save the data for that I'll attach data to product Dot product name which is coming from model likewise we have to check the price this model dot price finally we have to save the changes written okay that is Success message so this is the custom message which we are going to return so this block I can keep it inside the try catch we can take this exception message here I can use it here finally we have to create one more action method for delete product I will check the product is available or not first find here we have to pause ID which is given by the user with product is null then try to return the not form so this is the custom message which we are going to return if product is not null then we are going to delete the record three more I have to pause the product here and I have to save the changes finally return the message as okay this is the status which we are going to proceed to the user this time I will make it as deleted so we'll keep this inside try catch block so if there is any exception we are going to display that exception message here we build the application once so there is no error build succeeded to test our web Bap we are going to use this Swagger which is available inside the dotnet core application run the application here we are facing one error related to swag we'll see how to fix this error first here we have one action method called get to read all the products from the database and also we have another action method with the same name get and here we are passing the ID so there is a conflict between these two action methods so we have to fix this to fix this here we have to pass the ID I'll make it as ID it's a simple solution to fix this Swagger issue now run the application so now we are not receiving that error this is the simplest way to fix the issue so we will try to get the data whether it's working fine or not click on write out we will execute here what currently we don't have any records inside the products table so we are getting response like products not available here we have to pause all the parameters ID is not required since IDs identity field we don't need to pause that here I will pause mobile is my product name here I'm going to pass quantity click on execute so I'm getting status code 200 and product created again we will go back to execute this get action method click on execute here there is a conflict between data type execute record is inserted here we'll check this data type is float and here we have to check again our data type is float so I'll make it as double and we'll see run it again here we are able to read the data from the DB so now we will try to update the product go to the put and try it out here I have to pause product ID also product ID is one in our case we'll make it as one and pause from mobile I am changing it to laptop I'll execute here product details updated and we got the status code 200 we will check the database here So currently we have mobile now we modified as laptop so it's updated perfectly we will try to get the data based on the ID go to the try it out here we have to pause ID product ID is one click on execute so we are able to read the data here instead of giving one I am giving some product ID which is not available inside the DB click on execute we will receive the message like product details not found with ID 789 so this validation also working fine and now I will try to delete the product go to the delete here we have to pause the product ID I'm passing as one execute product details deleted if you check from get action method currently we don't have any products we can check inside the DB also so there is no data we will try to add few products here ID is not required while creating new product execute product created and reset it and I'll create one more execute product created we will try to read all the data execute here so we are able to read all the products here that's it for today if you like this video please like And subscribe to my channel thanks for watching
Info
Channel: CodeWithGopi
Views: 13,171
Rating: undefined out of 5
Keywords: api testing, api testing using postman, asp.net core web api, codewithgopi, crud, crud operations in web api using entity framework, crud operations usin web api and asp.net mvc, entity framework, restful, restful api, using entity framework with web api, web api crud operations with entity framework, web api crud using entity framework, web api tutorial c#, webapi, what is an api, what is api, .net core, asp.net, .net, api testing tutorial for beginners, mvc, entity framework core
Id: nSHi9fwrue8
Channel Id: undefined
Length: 25min 51sec (1551 seconds)
Published: Mon Feb 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.