Working with multiple tables | asp.net core MVC 6 tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is sahana this is our esp.net core MVC web application in this application we are using SQL Server as database Entity framework core as orm that is object relational mapper and also we have implemented repository pattern here is the project structure even if you don't know what we have implemented in this application you need not worry I'll help you understand if you want you can refer to asp.net core MEC playlist you will find all the videos over there just a quick recap inside dependencies you are you can find a folder by name packages here inside packages you can find these two Entity framework code.skill server and Microsoft dot entity framework.tools we have installed these two packages using package manager console right now we have single table that is tutorials table here you can find the folder by name context if you expand you can find tutorial DB context in most of the cases you can find this file inside models folder just to make sure just to prove the fact that we can keep it outside I have created a separate folder and we have this file here choice is yours you can have it anywhere in this application we are following code first approach you can see that right now this application has single table that is tutorial stable I want to have one more table that is article stable my requirement is here we have a drop down which lists all the available tutorials when I click on any of the tutorials let's see if I click on C sharp it should list all the Articles related to C sharp we are going to start by adding new model article the reason is in our application first we are creating model then with the help of Entity framework code we are creating database table right click on the models folder add class and name it as article click on ADD next step is to add the properties that will describe this model right now I have three properties article ID article title and article content now I'll make these two Fields mandatory by using data annotation we are getting error the reason is we have to import the namespace we have to import system.componentmodel.data annotations next we are going to set up relationship with tutorial table in this case tutorial ID is the foreign key and this property we call this property as reference navigation property this property holds reference to other related entity in this case tutorial model is the related entity same way I have open tutorial model now I'll set up relationship with article modem here this horse list of articles at models level our job is done our next step is to propagate these changes to database let's see how to do that I have open tutorial DB context file here I have defined DB set property for tutorials same way I am going to Define one more property that will represent article model I will see the database with initial data now I have to open Package manager console we should run the command add migration gives a meaningful name hit enter build succeeded now we have to update database to update database we should run command update database database has been updated without any error our main reason to have this article table is to store all the Articles so that when we click on tutorial we can display all the articles that come under this tutorial let's see how to implement that functionality now I'll create one more interface by name I article repository right click add class and name it as I article repository add it's not class its interface so I'll change this to interface this interface will have these methods as this is an interface these methods will not have implementation here we are going to create different class where we are going to implement these methods again right click on the repository folder add class name it as article Repository press Ctrl Dot choose Implement interface work with article table we need an instance of tutorial DB context how to get the instance to get the instance of tutorial DB context we are going to make use of dependency injection I will declare a private field which is of the type tutorial DB context then I will create a Constructor shortcut is CTR double tap to this Constructor we are going to inject the dependency that's all we have injected the dependency now we can work with this instance to get all the Articles we can simply return all the articles if you want to return the articles by tutorial ID same way you can return the articles but here we are filtering the articles by tutorial ID for article repository is ready next we need controller and view right let's work on that right click on the controllers folder add controller choose MVC controller empty and name it as article controller add here we have index action method I'll keep this index action method as it is I will write one more action method display articles but I want to display articles by tutorial ID I want to call the methods from article repository again I'll go for dependency injection and get the instance of article repository we are going to follow same steps see here we have injected the dependency from this display articles action method we are going to call the method get articles by tutorial ID here we are passing ID and it will return all the articles and we are going to return this from View our display articles action method is ready now it's time to create a view click on views folder add new folder this time we are going to name it as article we want to create a view by name display articles right click on article folder add click on view razor view empty click on ADD name it as display articles click on ADD from this line we know that our view is going to receive some data our first step is to specify model model name is article to keep the things simple going to display data in the form of table here is the code can you guess why are we getting error the reason is if you look at this action method we are returning a return type is of the type I enumerable article but here here the model type that we have declared is just model to fix this error we should make it will be of the type by a numerable our next step is to call this view if you have watched previous video you may know that we have implemented this Dynamic navigation menu using view component we have that view component inside views shade components folder inside components folder we have tutorial menu folder here we have default.cs HTML here we have a code that displays Dynamic navigation menu as a drop down we want to display articles for each tutorial right so here we are going to specify controller and action methods I'll specify the controller using ASP controller controller name is article now we can specify action method name action method is display articles we should pass one more thing that is tutorial ID we make use of ASP wrote ID and we have to specify the value now let's test our application okay this is our application we have tutorials here I'll click on one of the list see we are getting error do you know why are we getting this error the reason is here we are requesting dependency injection container to give the instance of I article repository but dependency injection container don't know which in which instance to give fix that we should go to program.cs file if you look at this statement here we have registered I tutorial repository service whenever we request the service I tutorial repository we get the instance of tutorial repository the same way we should register I article repository service with dependency injection container so whenever we inject the dependency we get the instance of article repository let's save and test our changes now fine let's test it again I'll click on tutorials asp.net core nice reason why we are not able to display any data because we don't have data related to asp.net core MEC tutorial we have data related to c-sharp Let's test that I'll click on C sharp see here we have C sharp related data if I click on JavaScript we don't have any data thus we have implemented new functionality in our application we have followed too many steps things may be a bit confusing for you if you want to go through the code you can download the source code from my GitHub repo you will get the link in the description you can go through the code if you get any error you can comment me I'll help you to fix that error I hope that things are clear see you soon in the next video thank you
Info
Channel: Just Pick and Learn
Views: 5,852
Rating: undefined out of 5
Keywords: just pick and learn, pick and learn, Asp.net core MVC tutorial for beginners, asp.net core MVC, view component, how to add new table to existing application, how to wotk with multiple tables, efcore, sql server, dependency injection, repository pattern
Id: 1yqv01iP-ck
Channel Id: undefined
Length: 10min 32sec (632 seconds)
Published: Sun Feb 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.