Full CRUD Operations in Asp Net Core MVC using Entity Framework Core with Repository Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back in today's session we are going to perform crowd operations with repositive pattern in asp.net core for that I am going to create a new project I am using visual studio 2022. I'm creating a new project here I am selecting asp.net core web application with MVC we are going to perform crowd operations with MVC so this is my project name and I'm going to store in this location I am selecting the latest.net framework I'm creating project now so I am going to rename this as UI layer this is our UI layer I'm creating another project for domain I'm going to select class Library here I'll name it as core this will be our data access layer which we are going to create our database entities and the DB context we will create another layer for business logics I'm creating another project here again I will select class Library this time I will make it as infrastructure create the project it has been created I'm going to delete the existing default classes so here we have three layers this is UI layer and this is the core layer which will be used for data access and this will be the our service layer you can call it as infrastructure also and here we are going to store all our business logics in this session we are going to use Entity framework DB first approach here one database available like crud with repository in this we have one table called products inside the products we have different columns like ID is a primary key which we no need to pass any value it is a identity field and product name price quantity these are all the fields available and we are going to create a model for this table for that I will go to the Core I'll right click and add a class here for our product model here I am adding product this is a public class and I am going to define properties here say ID another one we have as product name this is going to be required it's a mandatory we have price which is decimal also we have another property called quantity this is also required field product name I will Define with max length of 50 characters so in the database we have defined worker 50 so I am giving max length as 50 characters here then I am going to create appdb context to communicate with the database I'm creating new class here I'll make it as my app DB context this is the DB context name this will be inherited from DB context for this we have to add few namespaces related to Entity framework core I am going with the minus nuget packages I am searching for Entity framework core here I am going to install Entity framework core which is having latest stable version install it here I am going to install entity framework.sql server packages it's installed now I can add the required namespace here which is Entity framework core and here I can make it as public click on DB context go to the quick actions and I am going to generate a Constructor with options here we have to add our product DB set it's a product now to communicate with the database we need connection sync for that I will go to the UI layer I am going to app settings.json here I am going to add connection string first we have to provide the connection string name here we have to provide server then database name then we are going to use the windows authentication for that I can make it as integrated security as true if you are using SQL Server authentication then you have to use the username password Here and finally we have created https application so we have to use the Trust server certificate as true and here we have to pass the database server name so here I can copy that here I have multiple SQL instances that's why I'm using SQL instance name also if you have single SQL instance then you no need to pause this you can pass only server name alone this is my SQL server name and this is my SQL instance name here I have to pause database name I'm copying from my SQL Server I will position this now we have to register this connection string inside our program.cs open the program.cs so we have to reference with core project for that I will right click on the project add go to Project references here you can select the project reference because we have the database context and the model is available inside core project so that's why I added the reference now here I am going to register my connection string services add DB context here we have to provide DB context name this is my DB context name options here we have to provide options dot use SQL Server we are going to use the SQL Server here we have to pause connection string Builder Dot configuration we have to get the connection string here and here we have to pass connection string name so I can take it from App settings.js this is my connection string name I can close it here now I'll go to the infrastructure project here we are going to create our repositories for that I will create two different folders here to store the interfaces and implementations I am creating one folder for interfaces and another one I will create for mentations here I am going to create one interface I'll name it as product Repository it is going to be public interface here I will Define all the methods to perform the crowd operations first type first I want to read all the products from the product table for the tile create a method here which is going to return list of products here I have to add the project reference I'll name it as get all I need one more method to get the data based on the product ID for that I'll create another method get by ID here we have to pause product ID I'll create another method to update the data I'll make it as and here we are going to pause the product model we'll make it as model and another method I will create for update product it's a model finally we have to Define finally I am going to Define method for delete the data again here I am going to pause product ID for this now I am going to implement this interface inside this implementation folder I am going to create a class here to implement this methods I'll name it as product repository and I'll add it here again this is going to be public class which will be inherited from i product Repository here we have to implement interface here here we will receive all the methods so here we have to correct this it is get all method is going to return all the products from the product table so we have created three different layers this is UI layer which is used to create controllers and views we have created another layer for core which is going to be data access layer here we are going to store database entities and the appdb context so here we created another layer which is going to be infrastructure and we can call it as service layer also in this layer we are going to create all our business Logics here we created one interface and here we Define all the methods to perform crud operations so this is our interface we have implemented this interface inside the implementations folder so here we have all the methods so this is the basic structure of repository pattern now we are going to implement one by one first we are going to read products from the product table I will take this method to top to read the products we have to initialize our appdb context inside the product repository class for that I am going to create one private variable here I'll name it as context I'll create a Constructor here and I will inject this appdb context inside the Constructor now we can communicate with database by using appdb context here first I am going to read all the products from the product table so this is our product table which is available inside this database currently we don't have any products inside this table we will try to read the products from the products table where it will return the list of products I am making as YNC here I have to add a sink and it will be a wait I am going to return products now we will create a controller for products and we will read all the products from the controller for that I am going to UI layer I'll create a controller here I am going to create empty controller to read the products I have to initialize our product interface inside the product controller for that I'll create read only i product Repository we have to add the reference here it is coming from infrastructure project I'll name it as product repo I'll create a Constructor and I will inject this interface inside the Constructor I'll generate the Constructor so now I can communicate with our interface to read all the products from the product table here I am going to read products get all products so after reading the products I have to return to view here we are going to create a view for this here I am going to make it as YNC for this I have to add task here right click on the action method of index and create a view here I'm going to create empty View it will be index here I am going to add our model reference to read the list of products here I have to add project reference first tree it is available inside the core project it is product if you want you can create a separate view like product view model and you can access inside the index View here I am going to create title for our page it is a product list here I am going to declare one H3 tag to display our title inside the index View after this H3 tag I will add one horizontal row here I am going to display all my products inside the table so inside the table header I am going to display all our columns I'll create a table Row for this make a bootstrap class here here we have to add bootstrap reference for this project right click on the project go to the manage nuget packages here I am going to search for latest bootstrap version select the bootstrap and install it I'm going to install latest double version it's installed now so inside this I am going to add table headers here I'm going to use HTML helper and I will use display name for I'll read from model model dot ID I'll duplicate this required product name here then price quantity then I am going to add table body here here I have to bind all our products so before binding we have to check is there any data available inside this model or not for that I'll add one if condition here model should be not equal to null and model should have any data then only I am going to bind the data else I am going to display message like there is no product here I will create table Row for this I'll add table data we have four columns here if the data is not available then I am going to use the call span of four and it will display the message like no products available at this moment so this is the message I am going to display if there is no product inside this model if there is any data inside the product model then I am going to bind it here so if model is contains any data it will comes inside this block then I have to use the for each Loop because we are going to receive list of products here I'll use the forage and here I will be having product from the model here I will use table row then table data and here I can call product Dot ID first product name price then quantity build the application once so build succeeded there is no error run the application I am going to call product controller here we are facing one error related to our interface which is invalid operation exception unable to Resource service of type this is our project name and this is our interface while attempting to activate so here inside the product controller we are calling this interface whereas we have not added the dependency injection inside our program.cs so I will tell you how to fix this error so this is the error we are facing here I'll close this tab open the program.cs so whenever we are using any interface we have to add the dependency injection inside our program.cs here we have to add the dependency injection for at interface I am going to add here Builder dot services Dot I am going to add scoped and here I have to pass my interface name that is I product repository and I have to call my class name here I to post class name here now build the application once again I'm rebuilding so I will add one now link for product controller I'll go to the layout here I'll copy existing now item I'll give my controller name here it's a product controller and action will be index products run the application now here our now item is available for our product controller click on this products since we don't have any products inside our product table that's why we are getting this message as no products available at this moment so we have validated here inside our index view if there is any product inside our model it will comes into this block and it will bind each product if there is no data then it comes to this else block and it will display this message here and we will apply some bootstrap classes to our table here I'll add some classes here I add table and add border also I'll click the products now So currently we don't have any products that is the message we are displaying now we fixed our table style also now we will try to insert some dummy records inside our products table I'll open this product I'm going to edit this table I'll add here some products ID is a primary key and it's identity field so we now need to pass any value inside that ID here I am passing product name then I'll pause the price here I'll pause quantity currently we have two records inside our products table I'll reload this page so we are able to read the data as per our expectation here instead of displaying product name and I can change the display name here for that I will go to the model here I'll add the display name I'll run it again so now overheader is correctly showing and like this we can read the data inside our repository pattern we are going to create new product here we will add one button to create new product inside our index View whenever user clicks on that create button so we have to display the form to create these product details first we are going to implement this method to insert the data for that I am going to open the product repository which is available inside the implementation folder I am going to implement this method here we are going to receive the product model I am going to add context Dot products dot add I am going to add the model and here finally I am going to save the changes here we are using async methods for that I have to add async then I have to use a weight here this context dot save changes instead of using in each method like we need to use it for add update and delete I am going to create a common method for this I'll extract the method I'll name it as save this save method will be called in the data manipulation like add update deletable whenever we are calling these methods that time we are going to call this save now we are going to implement this add method inside our controller I'll go to the UI layer open the product controller here I am going to create a new method for adding the data I'll make it as create it will return the view here here we are going to create empty form to insert the data this is going to be get method now here I am going to create another method for post the data to the repository I'll make it as HTTP post here we will receive the product model I'll call product model here first we have to check whether model state is valid or not if it is valid then we are going to insert the data product repo dot add here we have to pause our model which we are going to receive it from View we have to add the project reference for product model this is available inside core project this is a product model so that's why I am referencing that project to this controller if model state is invalid then we have to display the message which I will save it inside the temp data I'll call this as error message here I am going to send custom message like so invalid model otherwise we can make it as modal status invalid so this is the message which we are going to return to the user also here I can return view here this is not required once product added successfully then we have to written this redirect to action here I can pause index view or else we can pause like name of here I can pause that index I am going to add enter this code block inside the try catch if there is any unhandled exceptions are there we can display that exception to the user for that I am going to surround with try catch block here so if there is any exception I am going to display inside this error message here I can show x dot message and I'm going to return to the create View now we are going to create view for this right click on the create action method click on ADD View I am going to create empty view here I'll name it as create here we have to reference model you have to pause the project name so product model is available inside the core project I have to select the core that is the product here I am going to add title and use view data here this is my title here I am going to create a form method will be post because we are going to submit the form here and action I will mention as create here here I am going to use bootstrap classes I am going to use one div class I will make it as card and here I am going to create another div for card header here I am going to display this title inside the card header then I'm going to add one more div for chord body so inside this I am going to display our form form here I am going to create hidden field to store the ID asp4 ID then I'm going to create one D4 product name here I want to add margin bottom two here I'm going to add one span class for form validation LED class text Danger close it here I am going to duplicate this for other fields the required price and quantity then we need quantity here I am going to add one more D4 button here I am going to display the submit button type will be submit class I will add BTN BTN primary and I will name it as submit so this is the form to create the product so inside the index view we are going to add one button so on top of this table I am going to create one button here here I have to add ASP action here I am going to call create action method and here we have to pass the controller name you can pause product is my controller name here I am going to pause bootstrap classes vtn primary and here I am going to add margin bottom three this is create product save the changes run the application currently we have two products I will delete these records and I will insert new one [Music] currently we don't have any products that's why we are displaying this message I'll click on this create product button here we will place these two form I'll run the application I'll keep a debug Point here currently we don't have any data that's why we are displaying this message click on create product so our form is ready here we will do some changes for this header I will go to the create here I'm going to make it as align these text Center and then I'll make it as bold run it again click on products I'll click on create product so it's displaying correctly so now we will try to submit the form without entering any values click on submit so our validation is working fine and whenever I am adding any product this message should go so now currently it is working from the server side validation we need to implement the client-side validation also for that go to the create view here I am going to add this section of scripts here I am going to call the script weight HTML Dot gender partial here I have to pause the here we have to pass the partial View which is available inside the views shared folder so this is the View I am going to add this because all the jQuery validations are available inside this partial view if you open this file so all the jQuery validations are available here so that's why we are adding this partial view inside our create View now I'll run the application once again we will check whether our client side validation is working fine or not I'll try to submit the form now I will try to enter the data here that time that error should go so now our client side validation is working fine I'll enter the price it's working quantity I will make it as then submit the form here submit is not working we will check what is the issue right click and inspect so button type is submit we are using the BTN primary and the form also we are posting the data it's going to create this is the post method create this create only we will recheck again so once it is submitted it should go to our product controller it's not coming here we will run the application once again now it is hitting our break point which is available inside the product controller we will check what is the data coming from the product model it will go to the next step we will check whether model is valid or not it's invalid so we have to check what is the error here to the model state there is one error I will go to the values so product ID is invalid so here we added the hidden field for ID I will comment this I'll stop it now again I am running the application because while creating the product we know need of ID we can ignore the we will create product again laptop price quantity I will add 4 submit now it's coming to our product controller this time model state is valid so it's going to the product Repository and it reads returning into the index view so our product is created successfully since this is a synchronous method I can make it as a Sync here I have to add the task I'll add a weight here because this is asynchronous Method we can check it here so that's why I'm making this action method also a sync so once product is inserted we have to display the message as success message I will add it here before returning this is going to be success message here I will display as product created successfully this is the message which I'm going to display to the user and these messages we are going to call it from our layout page so that wherever we are calling temp data it will be displayed in the respective view for that I am going into the layout so I'll take this temp data key value before entering the body I will check if temp data is not none then I am going to use the bootstrap alert here and add booster I will add alert classes here it is going to be alert and I'll make it as alert success here I will display the message as success I will display this one also I have to add one close button to my alert type will be button these things already we have discussed in previous situations button close and I'm going to make it as display right hand in data dismiss it should be alert also I can so for error message also I am going to display like this if error message is not null then I am going to display that message here this time it will be danger and here also I am going to make it as error I'll run the application and we will test our model properties we will run the application and we will check this alerts are working fine or not and the application I am going to create a product here this time I will create one mobile product I'll submit the data product created successfully so like this we can display the messages and we can close it here also we can implement the Auto close also in next session we will discuss how to auto hide that message alert we can add some space here after this here we can add space in today's session we are going to discuss how to update the data by using repository pattern so this is the example which we discussed in previous sessions this is the data which we binded to this table whenever we click on this create product button so this form will be loaded here we can create the new product I'll go to my UI layer I'm going to product this is my index here I am going to add one more table header for action here I am going to add one table data I'll add bootstrap style for this we'll make it as Center here I am taking one div here I am going to display the hyperlink 4 to pause controller here in our case product is the controller here I am going to add the action so action we have noted created so we have to create this and here we have to pass the route ID that is product ID we have to pause I am going to pass it as ID that we will receive it from product S ID for this I am going to add class the text I will add it as edit so whenever user clicks on this we have to redirect into this action method first we have to implement our methods inside the product repository I'm opening this so this is available inside our implementation folder based on the product ID we have to return product data for that I am going to implement this method here I am going to make it as a sync here I'm going to find product based on the ID which is passed by the user so it will return the product details based on this ID and also I am going to implement this update method here this is also I am going to make it as a sync method so we will Implement both the methods here and also I am going to make this save also a sync so here I can make it as save changes async to update the data first we have to check whether this product is available inside the database table or not first I have to check that I am going to verify it here I have to pass the product ID which is coming from our model if product is not null then I am going to update the data here I am going to pause product name which is coming from the model and also I need price so this is the data which I am checking based on the model ID whether this product is available or not if the product is available then I am assigning this model which is coming from The View so finally we are going to add context dot update and then I have to pause this product for updation finally I have to save the changes this is also a sync method so I am going to make it as a weight so here also I have to use the same thing now we have to call this update method from our controller I go to the controller this is my controller product controller so instead of making us different action method for create and update I'm going to make it as single one I'll rename it as create or edit so this post method also I am going to make it as create or edit in index also we have to change these action methods so that we now need to write duplicate code again and again so we can make it as single if you want to use separate you can make it a separate in this case I'm going to make it as single action method for both create and edit so this is the action method and here we have five columns so I'm going to make it as 5 here I am going to implement first get method I'm defining ID and I'm assigning default value as 0 here if ID is 0 then I am going to return empty View here I have to pause empty product model if it is ID is greater than 0 then I have to load the product details which is product so here I have to read it from productrepo Dot get by ID here I am going to pause the ID so this is also a sync method here right to make it as task here based on the ID which is passed by the user we will get data if product not equal to null then I am going to return this View and I'll pause this product details if we don't have any data with this ID then I have to display the message here which I will store it in term data here it will be error message product details not formed with ID I'll pause that ID here and I am going to return these two index page this is not required now like this we are going to implement post action method also so if model state is valid here I am going to check model dot IDs zero then I am going to call this insert method otherwise I am going to call the update method wait that's it we have implemented both the methods now now we will alter our view changes go to the create we are going to make it as create or edit I'll rename this so here we have to change based on the model value if user is trying to create then we have to display this create product for that I am going to validate here model is not null and model dot IDs zero in that case it should display the create product otherwise it should display the edit product here we are going to receive the data from this product model if model ID is zero then I am going to display this title otherwise I'll display this edit product here I'm going to change the action as create or edit so this is my title also I will change this change background color info and this title I'll make it as to and the class I will apply as text Center here we need the ID I am going to uncomment this earlier we were using only create that's why I removed the ID field now here we are going to perform both the insert and update operations by default this ID will be 0 so here these buttons also we are going to change based on the model ID here I am going to validate the data if model is not null if model ID is zero that means user is trying to create a product I'll display this otherwise I am going to display this for edit I will make it as update we will run the application first we will try to create a product so it's going to create a Reddit here I'm going to pass the product name I'll try to submit here it's a product created successfully and the data is not loading that means data is not saving here here I have to use the await will run the application once again now we are going to create a product click on create product So based on the condition it is showing the header as create product let this submit so product created successfully and it is added to our table now we will try to edit this product click on edit it's showing as edit product and it is showing the button as update so I will make it instead of quantity 15 I'll make it as 10 first will check the database so this is data is added quantity showing currently 15 I am going to make it as 10 I will update it product details updated successfully and it's updated as 10 we will check the database also so update it perfectly so here I'm going to make some changes for this bootstrap alert we will set timeline to hide this bootstrap alert automatically otherwise you second click on this and close it instead of manually closing so we can set the timeline where automatically it will close after given time period for that I am going to open my visual studio I will go to my UI layer go to the JS here I'm going to create one custom Js for that right click add new item click on JavaScript here I am making s custom JS click on ADD here I am going to make it as set timeout and here I am going to create a function for that here I am going to use the jQuery to set the alert timeout here we can set the time mode I am setting as 5000 milliseconds it will be 5 Seconds and these custom JS need to add inside our layout page I'll go to footer after footer I am going to use this custom JS file also this footer I want to make it to bottom I'll add custom style here we'll set the position will make it as bottom 10 pixel I'll set it so that it will display in the bottom we'll run the application I'll try to edit this product I'll make it price as 75 000 I'll make it 84 as quantity I will try to update now so this message is shown and after five seconds it should close automatically as per our expectation so it's closed we will try to update another product click on update so product details updated successfully so its message is closed so alert is closed as per our expectation in today's session we are going to perform delete operation in asp.net core with the Positive pattern we are going to add one more button here to perform the delete operation whenever user clicks on that button based on this product ID we are going to delete the data go to the solution UI layer here we have the views first we have to change the index page we have to add the one more button to delete the product I am duplicating this here I am going to change action method as delete and here I'm going to pause the product ID this bootstrap style I'll make it as Danger I'm adding this as delete now I'll go to the Repository go to the infrastructure layer go to the implementation this is product repository where we implemented all our methods which we used for crowd operations here I am going to implement the delete method here I am going to make it as ynchronous method So based on the product ID we have to delete the product from products table first we have to check whether this ID is valid or not for that I am going to create one variable for this product I am going to check this table products here we have to pause the primary key value to find the data from the table if product is not null so in that case product is available then we have to delete the data context Dot products dot remove the product and here we have to pass the product to remove from the database finally we have to call the save changes that is save method we have to call it here so this is the method which we implemented inside the repository now we have to call this delete method from our controller go to the controllers this is a product controller which we are using here I am going to create one action method I'll make it as delete this action method also I am going to make it as asynchronous here we have to pause the product ID based on the product ID I'll get product here which I can get it from our repository that is product repo get by ID here I have to pass the primary key ID if product is not null then return The View and here I have to pause product model if product is null in that case we have to redirect to index action method written I am going to redirect to index and here I'm going to return message like I think already we used here this one this message I am going to pass it to the user product details not found with the ID which is given by the user this message will be displayed in the index View also we can keep this content inside the try cache block if there is any unhandled exception we can display these two user try catch block I will add it and if there is any exception we are going to display this to you so this is going to be exception message here also we are going to move inside try catch so this is going to be get method here we have to create one more method to post the data to the Repository this one I am going to make it as post I'll make it as a synchronous method here we will receive the data in the form of model here I have to pause the action name also which is delete here I will get the data from the view in the form of product model this one I am going to pass it to our Repository product repo dot I have to call the delete method and I have to pause model here so once it is deleted I have to display the success message here I can take it from our previous one product deleted so this is a message which I'm going to display to the user and it should redirect to index View this one also I am going to keep it inside the try catch so if there is any error I have to display this message and it should stay inside the delete View here we have to pause the ID here I can take it only ID instead of model we can take the ID here so we now need to get the data everything here because here we are going to delete only here we are going to perform only delete operation that's why instead of model I am taking only ID so if this ID is valid we are going to delete the data we will create a view for this right click on this action method click on ADD View click on razor view empty at I can make it as delete here I have to add the model project name which is coming from core and I have to add the model reference here and here I am going to add the title UC view data this is my title here I'm going to create one form and Method will be post and here I am going to pass action to delete the product inside this I will declare one div in previous sessions we have designed this one I'm going to use bootstrap Style inside this card header I'm going to display the title then I'll add another div for chord body here I'll create one hidden field to store the ID this text box I'm making us read only because we are going to delete the data and I am going to add label here product name I'll duplicate this for price and quantity here I'm going to add another div to display the button run the application now I'll make it this buttons to left alignment go to the index so here instead of Center I can remove this Center So currently we have three products here I am going to delete this product which is in the end click on delete so it is redirected to delete view here we have disabled all the controls because here we are verifying the data and we are going to delete it click on the delete product deleted successfully and its message showing product has been removed from the list I'll try to delete one more product here and instead of 4 I am going to make it as something which is not in the database click on enter now so product details not found with the ID which is passed by the user so this validations also we have implemented we will check the table earlier we had three products this product has been deleted now we'll try to execute the query again so that product is deleted so we will try to delete one more product here delete it delete confirmed product deleted successfully and this alert has been closed after given timeline so like this we can perform delete operation in the asp.net core by using the positive pattern that's it for today if you like this video please like And subscribe to my channel thanks for watching
Info
Channel: CodeWithGopi
Views: 12,982
Rating: undefined out of 5
Keywords: crud with repository pattern in asp.net core, how to use repository pattern in asp net core, repository pattern with ef core in an asp.net core api, asp.net core repository pattern crud, repository pattern in asp.net core 6, repository pattern asp.net core 6, crud with asp.net core, crud in mvc with entity framework, asp.net core mvc repository pattern, codewithgopi, .netcore, code with gopi, dotnet core, repository pattern, entity framework core, asp.net core, .net core, mvc
Id: 8HLFcfW9T44
Channel Id: undefined
Length: 84min 3sec (5043 seconds)
Published: Mon Mar 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.