Complete CRUD operations in Asp Net MVC using Entity Framework with Repository Pattern | Part - 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to my channel in today's session we are going to discuss about repository pattern mvc by using entity framework we are going to create three different projects like this one is ui that is web application and core application and infrastructure and this ui project will be interacted with the core and infrastructure to perform the cloud operations so we are going to perform crud operations in a simple table like this next we will see the structure of these three different projects first we are going to create one mvc project this is the web project that is called ui so this mvc based application will communicate with the core and infrastructure project libraries to perform the operations and secondly we are going to create one more project called core project this project will be class library project and here we will be having our database models or entities and we will create the interface also for the db operations inside core project and finally we are going to create one more project called infrastructure project this project also class library project here only we are going to perform our third operations before continuing with this session i would request you all please subscribe to my channel and click on bell icon for notification alerts so i'm using visual studio 2019 for this demo click on create a new project here we are going to create mvc application i'm searching for web application so i'm going to select dotnet framework web application click on next here we have to provide our project name that is i'm giving us my app and this is going to be ui so you can give any meaningful name here and i'm going to store this project inside my documents folder you can select the physical location where you want to store the project and selecting demo and i want to give my solution name as repository with entity framework because we are going to use entity framework with this repository pattern that's why i'm giving this entity framework i don't want to place this solution inside the same directory i'm going to place outside of this directory and you can select the latest dot net framework which you have in your machine click on create so here i am going to use mvc select mvc and click on create now our project has been created here go to solution explorer this is our ui project now we are going to create two more projects for that right click on the solution and new project here we are going to create class library project here we have to search for the class library project so here we can select the class library which is having dotnet framework and cshop click on next here we have to provide our project name that is our core project and we are going to store inside the repository with entity framework directory which we created for our ui project because this all three projects belongs to this single solution that's why we are selecting the same directory and here we can select the dotnet framework 4.7.2 which is used for ui also click on create so our project is created here i need one more project for our infrastructure right click on the solution add new project here we have to search for class library select the class library with dotnet framework and she show click on next here we have to provide project name again this is infrastructure and selecting same location and framework 4.7.2 click on create so our all three project has been created now so now we are going to work with the core project first here we have to delete this class which came with the project default one here already we discussed in core project we can add the database entities first we will add the database entity further right click on the core application add new item select the data we are going to use the entity data model and here we have to provide model name that is we are going to use product that is the product model so before this we will see the database first here i am using sql server for the database connectivity and i am using windows authentication this is sequel 2012 is my sequel instance name and this is my system name click on connect connect it to my database server expand the databases here we have one database called adivo example inside that we have table called tbl product master we are going to use this table for this demo right click select top thousand rules so this is the table structure we have product id which is identity column product name price quantity and remarks if you have seen previous examples already we worked with the same table structure so this is the product master that's why here we are providing model ms product model click on add so we are going to use entity framework designer from database click on next here we are going to use database first approach already via the database click on new connection here we have to provide the server name which is available here properties so this is the name i have to provide so this is the database we are going to use for this demo click on test connection connection succeeded click on ok so this is the connection string which it is going to be saved inside the our app.config or web.config here we can rename it as product entities click on next so we are going to use the latest entity framework which we have 6.0 click on next here we are going to work with the tables alone that's when selecting table and here also i will provide as product model click on finish so now our entity model has been created so after adding this product model automatically our connection string is added into the our app.config so same connection string we have to add inside our ui also because from ui only we are going to connect with the application for that we have to add this connection stream into our ui also now we will create one interface here right click add new item [Music] here click on code we have to create one interface here i am giving name as product repository so always interface starts with the i click on add this is going to be public method here we will create methods for our crowd operations like i want create method to create the product here we have to pause our tbl product master that is product and then i need one more method for update you can use same table here and i need one more method for delete here we are going to delete the product based on product id so we are passing product idle and here we have to get all the products for that i'll use ironing variable so that it is going to written as list of products get all products and i need one more method to get the product based on product id this get product by id here we have to pass as product id same value we are passing here we will build the application of core build once if there is any error we can fix it here so there is no error so now we are going to implement our business logic layer that is called our infrastructure layer expand this i'm going to delete default one which is created for us that class now i'm going to create one class here i'll name it as product repository so here only we are going to perform all our crud operations so this will be inherited from our interface for that we have to add the references first infrastructure will be referenced with core application so click on projects here you will get the project for click on ok now we can access all our core items from the infrastructure project like this we will add the reference for ui also because we are going to access info and core projects inside ui project so now so now we created one repository class inside the infrastructure so this is our class this will be inherited from i product repository for this we have to add reference like my app.core so now we can access product repository here here go to implementation control dot implement all interface so here all methods will be implemented first here we have to call our db entities for these product entities db i'll create one constructor here city or product entities so first we are going to create product for that db dot tbl product master dot and here we have to add reference of entity framework for that right click on the references go to manage nuget packages go to browse here we need internet connectivity to add the nuget packages here we can look for entity framework select entity framework install click on ok we have to accept the license terms as you know to install any nougat packages we need internet connectivity then only we can install so our entity framework has been added now we can use add here so we are going to add product so finally we have to save the changes next we can perform delete also here first we have to identify the product based on product id that is tbl product master this product here we can use the find to find out the product so here we have to pause our product id based on product id it will search the product and if it is found then product master dot remote we can use the remove here we are going to pause project so finally again we have to save the changes then we have to get the product from the product table so directly we can get the product from the tbl product master that's it no need to put so directly we are getting the product from the product table and based on product id we have to filter the product for that product equal to find so based on product id we will get the product and you can written product number so also here we used this method same thing we can use it here also if you require you can use any of them it will work and finally we have to update the product for that here we have to use the entry of product you have to change the state system dot data dot entity entity state dot modified that's it now we will build the application once build there is any errors we can fix it here so there is no error already we referenced both core and infrastructure projects with our ui project now we can go to controllers we will create a controller here right click on the controllers click on add controller here we are going to use controllers with views using entity framework so that we no need to create any view manually automatically it will be created by using scaffolding mechanism click on add here we are going to select model class that is our tbl product monster here we have to select the entity data context controller name i am giving as product you already aware of that so all the controller should contain controller at the end of the controller name and here we selected generate views so that automatically views will be generated for us click on add so it's installing request to get packages so there was an adder turning the selected code object reference not set so we will build the application once core application we will build once and infrastructure also we will build once and first we will build the ui build succeeded now we will try to create a controller we are going to go with we are going to use views using entity framework and select model this is my model this is my entity and controller name will be call it controller right now our controller has been created with all action method this is the index action method to get all the products this is the details based on id we are getting the products and this is the create view action method and this is the post action method to save the data and this is the edit to update the data and this is the viewport and this is the post method of edit to update the product this is delete u method and this is the first method of delete to delete the product okay so all the action method has been created by entity framework we no need to create anything manually now we will run the application once so our project is running now we will try to access our product controller so this is the final output of this session so we can create product by clicking on create new here i am going to add like some create so product has been created now i want to edit this i'm updating this update is working fine i can go to access the details from here we can edit the product to list also i can delete any product from here it will ask for the confirmation click on delete deleted successfully so here i'm going to wind up this session to apply any bootstrap styles you can add it in previous sessions already we are shown all those things and whenever we are creating product once it is submitted you can show the model pop-up or a message box here for the user information and you can you can apply whatever the changes you want to apply and also we are not going to put any validations here it is everything controller is created by the entity framework we are not going to add any uh like exception handling under in previous sessions already we covered exception handling and how to validate and how to add the bootstrap styles to the our view we discussed in previous sessions if you want you can go through with the previous sessions and you can apply whatever the changes you want okay if you have any issues with this example are this code if it's not working or if you are facing any issues you can put me in comments so that i'll try to reply as early as possible that's it for today thanks for watching this video you
Info
Channel: CodeWithGopi
Views: 7,760
Rating: undefined out of 5
Keywords: basic database crud operations, crud in mvc, crud operation in mvc, crud operation using generic repository pattern in mvc, crud operation using repository pattern in mvc, crud operations in mvc, crud operations with repository pattern, generic repository pattern c#, mvc, repository pattern, repository pattern c#, repository pattern c# mvc example, repository pattern in c# mvc, repository pattern in mvc, crud operations, .net core, entity framework core, crud, code with gopi, c#
Id: vuWGL3fN74k
Channel Id: undefined
Length: 23min 17sec (1397 seconds)
Published: Sun Mar 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.