welcome to this crash course on asp.net core MVC and Entity framework core Inn net9 this is a step-by-step tutorial covering the essential topics and foundational knowledge needed to start learning asp.net core MVC from scratch prolific Creator Alan Omari created this course if you want to learn asp.net cor MVC from the ground up this course will take you there we'll start by breaking down the entire MVC app structure exploring every file covering the main Concepts you need to know and then dive into working with databases using Entity framework core from crowd operations to complex database relationships we will be using the latest net version net 9 and it's going to be super simple well welcome to code the future my name is isan and I help you learn CP and.net on your own now let's get into it I'm going to explain to you what the MVC architecture is in programming and I'm going to walk you through everything you need to know MVC stands for model view controller and it is an architectural pattern that divides an application into three primary components models views and controllers this separation helps achieve a clear division of concerns in our applications separating concerns or responsibilities in MVC makes the application easier to develop debug and test because each part either the model the view or the controller has a specific job when responsibilities are mixed like combining user interface and business logic changes to one can affect the other making updates and testing harder and more error prone for example user interface logic changes often so if it's combined with business logic you'd need to modify and retest both every time the interface changes now let's see each of these components in more detail the model in MVC represents the information or the data the application operates on and handles the logic for processing this data for example if you have an application for managing books in a library the model would represent the data about the books and the rules for adding updating or removing books from the collection a view in MVC is the part of the application that displays the data to the user a controller then manages user input and interaction determining which model types to work with and which view to render effectively controlling the application response to requests I'm going to show you how to download and install net 9 and the latest version of Visual Studio which is our IDE in order to be able to work in net 9 so let's get into it so I'm just going to click on Google download. net9 and we'll see the first thing we have here in the Microsoft's website we should see we should be redirected to the page that allows us to install the net 9 SDK in the time of recording this tutorial we have the fifth preview of net 9 probably when you will see this it will be fully out the final net 9 version but this is what we'll use and there shouldn't be many changes with the final version so here we have 9 preview five depending on your operating system here just choose the correct one for you I using Windows and my processor is a 64 processor I'll select this option right here and we'll wait foret 9 SDK to be installed so it is already installed let me click on it and click on install here okay the installation was successful let me close this window here and now if we just go back to the page we previously were we can actually check what we go here to net P5 to release notes to see what version of Visual Studio net 9 actually requires if we scroll a bit below we will see that9 is compatible with Visual Studio 17.11 latest preview and if I click on it can basically download the latest version here in this part by just going to community 22 to download the free version and the downloads start right here but I actually have the visual studio installed and I could have just updated it on my application but let's see how it works right here if I click on it I should receive the option to actually just upgrade continue now the visual studio installer is opened and it gives us the option to update to the latest version I'm just going to click on update and the download starts give it a bit of time now after a while the visual studi latest version is already installed as you can see here so we have the latest version of net the latest version of visual studio and we are ready to go now one last thing you want to do if you are using a preview version like myself is actually going to need to go to the settings to visual studio to allow this previews so let me open up visual studio click on continue without code so I don't need to open a specific project go to tools go to manage preview features and a bit below we should see an option here we should click on this option here use previews of the net SDK this enables the ability as we can see here to consume preview sdks by default if I click on I click on it click on okay if we restart the visual studio then we should be able to work with the preview version of net9 you need to know the contents of each file in your project when building anything in programming for this reason I'm going to explain to you concisely the role of all file files you have when you create an MVC web application project in as.net core to create our first application you can just open Visual Studio and click on create the new project next find an mvis template for a web application and make sure that c is the language for this template not f for example in the page that loads give a name to your app like my app you can change the location where you want your project to be stored if you want as well click on next and here I will select the net version to be net 9 this preview version but the final release of net 9 may be available by the time you watch this tutorial let's leave all the other settings as they are for now and just click on create here we have our application when you create a new asp.net core MVC project several key files and folders are automatically generated let's go through them one by one first we have the program. Cs file this is the entry point for the application this first line initializes a new instance of the web application Builder class which sets up the configuration services and the web server next we add services to the dependency injection container here we're adding MVC services to the container with support for both controllers and Views this allows our application to handle incoming HTTP requests and render HTML views then we build the application this line compiles the app creating a web application instance which you can then configure and run now we configure the HTTP request pipeline which determines how requests are processed by the app if the app is not in the development environment we set up an exception Handler to redirect users to the home/ error page when an unhandled exception occurs additionally we enable HTTP strict Transport Security to enforce secure htps connections we continue configuring the pipeline with this app. use https redirection which ensures that h HTP requests are redirected to https and app. user routing enables routing which allows the app to match incoming requests to the appropriate endpoint next we have the app.use authorization which is responsible for authorizing users to access secured resources we map then static assets this line enables serving static files such as images CSS and JavaScript from the www root folder the then we configure the default route for the NVC application here this sets up the default route pattern which maps to the home controller and its index action method by default the ID parameter is optional so this is the default route that will be redirected to when we start our application every URL is actually assumed to have this pattern the first part is the controller then a method of that controller which is actually called an action in hpet core and the ID that is optional let's say a request is made to a URL called SL items SL overview our application will try to call an action called overview inside an items controller and now let's go to the last line finally we run the application this line starts the web application and begins listening for HTTP requests now let us now see what the other folders do as well just as a reminder if you want to to get the fundamentals right while learning C you should check out my ebook which is on sale now it will help you with whatever you're building in.net and save you hours of research by providing all the C insights you need in one place this book will be a valuable resource for everything C related and you can find the link in the description if you want to check it out now back to the video the controllers folder contains the controller classes these handle incoming HTP requests process user input and interact with the model to return the appropriate view for example we have the home controller here that is already created the models folder then contains classes that represent the data of the application here we have a sample model error view model. CS which is used for error handling the view folder contains the Eraser View files or the dot CS HTML files used to render HTML to the client in this folder you'll also find the view imports. CSH HTML which contains directives that are imported into every view such as tag helpers and the underline view start. CSH HTML which specifies common view configurations like the layout within the views folder there's also a shared subfolder which contains shared views like the layout which is the layout page that is used as a default design for all pages and this underline validation scripts partial CSH HTML file which contains client side validation scripts there's also a home folder which contains views related to the home controller such as the index view page and the Privacy view page the www root folder is the root for the application static files like CSS JavaScript images and other assets inside will find subfolders like CSS for stylesheets and JavaScript for JavaScript files for example site. CSS and site. JS are default files included for your custom Styles and scripts next we have the configuration files the app settings. Json file is the main configuration file for the application it contains settings like connection strings and apps specific configurations which we'll see later on let's take take a look at the properties folder as well where we have the launch settings. Json file that contains settings related to how the application is launched during development they can be modified to suit the specific requirements of the project or different development environments and that's a quick overview of the main files and folders in an as. netcore MVC web application project I'm going to show you the basics of asp.net core MVC using net 9 we will see how to work with the models the controllers and the views and how they interact with each other all in this short video if you're new to MVC this is the perfect starting point I'm just going to go to the models folder and create a new model or basically a new class for the models I'll name it let's say item. CS we'll see that the simple class will be created now this class represents the data of our application if if we had were to store this which we'll do later on in a database this class that's stored in the models folder will represent a table in our database and each property that this class will take will represent the different columns that the table will contain so we can give to this class different properties let's say let me just give it an ID property firstly and I'll explain everything so basically we're setting an access modifier to public for this property so that it can be accessed outside the class the ID we name the property here and it will be an integer this is the type of data and here we have a getter and a Setter that allows this property to be accessed outside of our class so this is an auto implemented property this is just some c Theory here we can specify here different properties for our model and we give each item an ID we can give also each item a name I'll set it to be public string the name will have the data type of string I'll name it name and the getter and Setter as well now let's say we want to access this item in a specific URL pattern let's say that it would be slash items slash let's say overview so we'll create this application so that when a user makes a request to this URL it will redirect it to a page that gives him the item for this reason what we're going to need is we're going to need a controller named items and also an action in that controller named overview let me just go to the controllers folder and add a new controller I select an empty template here click on ADD I'll name the controller to be items controller and we see a simple class here an items controller the items controller class which inherits from a base controller class basically every controller will inherit this base controller class because it inherits multiple methods but you don't not need to worry about this for now we can see as we talked before each method in a control ER is called action in as. netore MBC and here we see this index method or this index action here this is a public method it returns an i action result and it's named index I'm going to clarify to you in the next section what I action result means in more detail but basically just this just allows our method to return different types of things in this case we are returning a view which is actually included in this action result type but as I said we'll talk about the action parameters and action result types in the next section for now what we'll do is that I'm going to actually change the name of this action to be overview since we're going to need we wanted the user to be redirected to this specific route and I'm going to take an instance I'm going to create an instance of the model that we just created here inside this action picking a variable naming it item equals to new item and I'm going to need to import the module on top of the controller so that the controller knows the model basically that we created before so I'm going need to use this using directive the name of the application models here and as you can see this is this item this item keyword is now in green because the page knows recognizes what it means and let me just create one item here it's not necessary for me to give an ID to the model because the program will give it itself so I'm just going to specify for the item the name property and let's give a name to an random item let's say keyboard and that's it so I have created an item here and I'm going to pass it actually to the view so that we can be able to render this item that I created here inside the view that we'll basically need to create what this action here does is that it creates the item it renders The View what we're going to need to create the view because we don't have a view named overview yet we can just go to the Views folder we're going to need to actually create a folder to store all of the views inside the items controller for this reason I'm going to name it items and inside this items folder we can store views for each action in this controller and for our case I'm creating an empty View and I'm naming it according to the name of the action which was overview and here we have our overview CS HTML page here you can write the plane code plain HTML code that will be rendered to the user but we can also embed here C code which we'll see in a bit what we want to store in this page I'm actually writing a HTML paragraph here and I'm going to display to the user this is the name of the item and for me to actually access this item mode model that we created inside our controller here that we put as a parameter in the view I'm going to need to I'm going to need to import the specific model on top of our view page I'll do it by just writing this add sign model and then the exact address of the model that we created which is the name of the application that models do the name of the model item and now I can access the item that we gave into the view by just writing this at sign the add sign will use a lot this is how we basically write C code inside our view pages and for me to access a model I need this model keyword and add model and here I can access the ID or the name of our model I'm going to access the name here in this case and now if we run the project let's see what we have written now now this is our home controller the index page the default route of our application so this is what we'll see here but if we go to slash items slash over view we will be redirected to the items controller to the overview action which will actually render our overview view page let me just hit enter and we should see here this is the name of the item and then the name of the item which we specified by using this C code in our view page so this is just the basics it may seem a bit complex right now but you have worked with models controllers and Views all in this short video I'm going to explain to you what I action result is in asp.net why we use it and I'm going to walk you through everything you need to know in the previous section we saw inside our controller that the actions return this I action result but what is the I action result I action result is an interface that basically defines a contract for all the different kinds of results that an action can return inside of an action we can return to the user different results we can return a page we can redirect the user somewhere we can return adjacent just a simple message and so on all of these different action results derive from the I action result interface in some cases we may have a condition inside our action which would return different information to the user depending on specific requirements and in this case for us to include each of these different return types in the return type of the action we specify here I action result instead of just specifying let's say viewer result our content result so that it includes everything that the action can return let me just remove this and show you what we could use if we didn't use the I action result if we would want to just return a view let's say we can just return here A View result and then return the view here actually view the view that we are returning here is just a helper method that is inherited from the controller class and it's just a short of this view result the long way of writing it would be return New View result but as I said this is a helper method that allows us to write it a more flexible and short way and this is the way you will mostly see it in the code or from the documentation so we could return different kinds we could also return other kinds of information inside a action we could return content result and here I can just write return content content is as well well a helper method here I can just write a message to the user like hello there we can return just Json result a redirection result and so on but every of these different result types inherits from the I action result and as I said that's why we basically just write on top of as a return type of every action I action results so everything is included in it I'm going to explain to you what action parameters are in asp.net core and the different kinds of them I'm going to walk you through some examples as well so that you'll learn how they work so let's get started action parameters are way for actions to receive input data this input data can come from different sources such as URL segments query strings form submissions or even a combination of some of these let's just start to see how the URL segment parameters work in our items controller I'm just going to create a new action so public I action result I'm actually going to name it edit and here in this action I'm going to receive an in ID parameter from the URL and in this case the parameter is passed to the action directly from the URL depending on the route configurations of our application if you remember in program. CS the default route pattern here is specified and it actually allows us to receive an ID parameter okay so let's go back to the controller and I'm just going to return on the screen some content I'm going to write here return content and I'm just going to return basically a message on the screen so I will return the ID then I'm going to pass here the value that we get or the parameter that we get from the URL let me just run our project and if I go to this specific route to to items the name of the controller slash edit slash let say 2 3 1 whatever number you want we'll see that the parameter is passed to the action from the URL directly and then we have displayed this parameter on the screen so we can see here that the ID is equal to two now let's just see how the query string parameters work so question string parameters are this part in the URL that comes after the question mark and they usually used to filter or to sort this method is usually used to filter or to sort data actually let just see here how it works so after the edit part we'll input here question mark and give the ID the value of let's say four click on enter and we can see on the screen that the ID has the value of four so the parameter is passed from the query string to our action and we see here the value of the D on the screen now another way would also be as I told you before to take data from form submissions but do not have the necessary knowledge yet so I'm just not going to take an example for it and one last thing I wanted to mention is that if we would actually change the name of the parameter we take from our action here let's say change it to item id we would be able to still get the the data from the query string but we would would not be able to take this from the URL segment just because in the program that CS file here in the pattern of our route we have specified that we can take from the U the URL can take an ID parameter and not an items ID parameter so to say so if I just go back here run the project and let's say if I go to the route here items slash addit and I right this query string items item id equals to five we should still see the value of the ID printed in the screen but if I would do it using the URL segment like edit slash7 you would see that the value of the ID would default to zero just because we are not taking the value from the URL in this case Razer is a markup syntax used in asp.net core to embed C code into HTML it's designed to be clean and concise making your code easier to read and maintain you'll commonly see razor syntax in views which are responsible for rendering the UI razor syntax is denoted by the at symbol now let's look at some common uses first let's start with basic variable embedding suppose we have a variable that we want to display in our view in this example we declare a variable message inside this code block that we can write inside our view pages and use the at message syntax to embed its value inside a paragraph tag razor also supports embedding more complex expressions and C code blocks for instance let's let's Loop through a list of items and display each one of them here we use the add for each Loop to iterate over a list of items and output each item within an list tag notice how we seamlessly switch between C and HTML razor syntax also includes conditional statements which are useful for rendering different content based on certain conditions let's see an example with an eal statement depending on the value of the is logged in variable Razer will render either welcome back or please log in this makes our views Dynamic and responsive to the application State now let's talk about eraser directives directives are special instructions for the Eraser engine and a prefixed with the at symbol some common directives include at page at model and at using for example the at model directive specifies the type of model data The View expects this is crucial for strongly typed views in this example the at model directive is used to indicate that this view expects a my view model object we can then access the model's properties using at model lastly eraser has a feature called tag helpers which allows us to dynamically add behaviors and attributes to HTML elements tag helpers make our Razer views cleaner and more maintainable here's a simple example using the asp4 tag helper the asp4 tag helper binds the input field to the name property of the model this ensures that the correct value is posted back to the server when the form is submitted so what is Entity framework core Entity framework core is a tool set foret developers that simplifies database access by allowing us to work with databases using net objects it eliminates the need to write complex SQL queries by providing a way to query and manipulate data using CP code Entity framework core supports various database operations like creating reading updating and deleting data making it easier to manage database interactions in an objectoriented manner there are two main approaches to using Entity framework or the code first approach and the database first approach in this video we will learn how the code first approach works the code first approach in Entity framework core allows us to define the database schema using C classes and then generate the database from these classes the database first approach works the other way around the code first approach is ideal when starting a new project without an existing database as it provides full control over the data model through code okay so we're going to start with the code first first approach and we are just inside our NBC web app template here in Visual Studio the first thing we're going to do is go to the dependencies here click with the right of the mouse and select manage nouet packages we're going to need to install the needed n packages in order to implement Entity framework ore inside our project so the first package we need here is Entity framework core the first one you see here click on install click on I accept next we need the Microsoft ntid framework core tools click on install as well and then the microsoft. Entity framework core. SQL Server since we are connecting a pro object with a SQL Server database with SQL SQL Server management Studio actually click on it and install so all of these packages that we're installing right now have necessary tools and commments needed for us to basically map our our C classes and models and generate a database and connect basically with the SQL Server database okay now that we have all of our three packages in sted let's just check the models that we have in our in in this project so here I have only like a simple item model with an ID property and a name property and we will store this model inside our database what we're going to need other than our model we're going to need a context class which as a practice is stored inside the data folder and I'm just going to explain to you what the context class is needed for but let let just create the data folder firstly just add a folder called data and inside the data folder we're going to need we're going to add the class I name it my app cont text or something with context in it click on ADD now here we have our context class we are going to need that our class inherits from The Entity framework course DB context class and let me see what we need to inherit here we need actually to inherit and the the framework core on top of the page I would suggest using ntid framework or on top of the page and this class basically contains many properties and methods in order for the context to do its work so the context is this class like the main bridge that connects our project with the database here we store every instance of the models we have in our project object and with the context with the method that the context provides it's possible to basically query data from the database filter them get the data or modify them what we're going to need after inheriting the D context class is we need a Constructor inside our context class and this will just contain a few generic parameters to enable the needed configurations for our context so these are the parameters that the that Constructor needs to have and what we also need to write here is a sety we need inside the context we store an instance for each model that we have in our project in this case we have this item model and we need a dbet instance which is this entity frameworker class and this will be for our item model we give it a name like items and we give it this getter and set so this is just like a property and enables us to access this model from the database and basically what we have in our project to start in the database we need to import the model folder on top of the page my app. models and this is it for the context the next step we need to take is to create a database instance in our server management Studio that we're going to use as a data base for project we can just go to the view menu here and click on server Explorer here we have all of the basically a lot of database instances I had created before we just need to go here to data connections click with the right of the mouse and click on create new SQL Server database here we need to write the server name and basically if you open up the the Microsoft equal server management Studio you will see the server name on top of the page if we can just click the name of it here connect the SQL Server management Studio to this to our server and go back to the project and put the name here we'll set we'll use the windows authentication to log on to the server the encryption will set to false you click on trust certificate here we are just selecting the authentication this is a default one we're selecting that we do not need to use a password to connect to our database basically here just some options just some configurations don't think too much about it and give the database a name like my app database click on okay and in the after the database we be generated we should see in this window right here the name of the connection string the connection string is the address of the physical database so basically this tells us where the database is located we should copy this and the connection string we need to input it in our app settings. Json file so that we then can connect our project to this physical database here we need to write connection strings and we need to give a name to the connection string I usually name it like default connection string it doesn't matter here we inut what we took from the input the name of the connection string just right here with the name of the server the name of the database as you can see here and we put the encryption to false and every and some other configurations last step now to connect our project to the database will just be to go to program.cs and add the service in the services containers basically this will connect our database with our project and with our context so I'll just add service here Builder services that add context the name of our context was my app context here inside we'll just inut a few options let me put this in a new line options do use SQL server and inside here we need to access that connection string to get the location of our database builder. configuration. get connection string I need to input here the name of our connection string connection string just close the parenthesis here so basically we are writing a a service to connect contacts basically with our database here just let me remove this dot here need to import and the frameworker on top we need to be add the migrations to our database a migration is basically just a class that will be created that is the code the representation of how our database will look like with all of our configurations for our models and the context that we we have already written here so to do that we're just going to need to go to tools package manager console here and just write command add minus migration and just give a name to the migration creation hit on enter so build succeeded and we should see this migration class here on the screen a migrations folder will be basically created the migrations track every change we do to the database schema and each time we change the models or the context we need to add a new migration to the database you can see here that the table will be created on a database with a columns an ID column with its specifications and a name column here the primary key is by default the ID property as we can see here and the last thing we need to do is to basically upd write the comment here update database and this migration will be added to our database right now the table a table for items should be generated already in our database if you go to SQL Server management Studio to databases let me find the right database of our product Pro which were my app database can click on tables we have a table for the migrations here and the the framework or migrations history which basically checks all the changes to the database schema that we make and right below we see the items table if we click on edit top 200 rows we should see basically an empty table for the table with an ID and a name click on design here to see our columns so this is how Entity framework course code first approach Works in this video we will learn how the database first approach works the database first approach in Entity framework core involves generating C classes and a DB context from an existing database schema this method is suitable when working with a legacy database or when the database design is managed outside of the application development process allowing us to integrate the existing database into the application seamlessly I in SQL Server management Studio here and I have a database and basically I have three tables here three prepopulated tables dishes table here an ingredients table and a dish ingredients table and I'm going to use this database to create the the models and the context in a new project so I have just created a new MVC web app project and I have already installed Microsoft Entity framework core framework core. SQL server and microsoft. Entity framework core. tools so you need these three packages to be able to work with Entity framework core and now what we will do is just go to package manager console here and write this comment scaffold Das DB context then here we need to input the connection string of the database that we will import so I have already copied it after this line we need to write here microsoft. Entity framework core. SQL then we need to write Dash context directory data so here we'll specify in what which directory our context will be stored which will be stored in a folder named data and then output directory here we specify the directory of where the models will be stored so they will be stored in the models folder and I will specify here the data annotation flag what data Antion is are basically this attributes that each property in our models St to basically validate them to specify the relationship with each other or how they will be stored in the database and if we specify this flag here when the models will be created the data annotations will be included in them so now if you just hit enter we'll wait for the build to succeed and the build succeeded if we open up our models folder here we'll see that in our models folder we have the dish model these are the data annotations that were included in our models we have the ingredient model right here and in our data folder we have our data context the dish ingredient helper model was not created as a separate model in our models folder but the relationship between dish ingredient dish and ingredient models was specified in the DB context in the on model creating method it's a bit different from how we typically specify it but it should achieve the same result C stands for create read update and delete and these are the basic operations included in every application we will perform these operations on our data in this project and show the changes in the front end we have already created a simple model and stored it in the database using the Entity framework course code first approach this is the MVC project we have only one model we created a context and we stored these changes to the database now one more thing I'm going to do before we start is that I'm going to add another property to our model just because it's a little too short but we'll still keep it short so that you understand everything better so that it is the most simple thing ever so I'm just going to add another property a double property named price for our item we have already created migration and stored it in the database for our project now that we made a change to the model we're going to need to add the migrations one more time to our database for this reason we just need to go to package manager console and click add migration and put a name here for the migration let's say second migration hit enter and basically this migration class that represents our database schema should be created that should be displayed in the screen and of course added in our migrations folder so we'll just wait for the build to succeed and this is the migration that we already created so new column was added to our database to our table in the database for the price and now just just need to click on to write the command update database to perform these changes to our database as well build succeeded so now we just need to go to our items controller and this is where all of the actions that will perform the needed operations will be stored I'm just going to delete these two actions we had created previously and first of all what we're going to need is for us to access the database we're going to need to import the context in our controller firstly I'm just going to need a private read only variable to basically store the context and we'll use this variable to do all sorts of to use all sort sorts of methods that we can use from our context but right now just let's create this variable I'll name it with the name of the context here so this variable will hold our context I'll just name it underline context just as a practice and inside the Constructor for our controller here we will take basically the context by writing the context here and a variable to represent it and then in the private variable that we declared above we are just going to need to just going to need to add the value of the context to our context variable that we'll use in the controller right here adding line here after storing the context in this variable now we'll start with writing each of the actions the first action it's going to be a public I action result I name it index for the main page which will basically just display all of all of the items that we take from the database and here we're going to return a view but we need to return a view with the items that we'll take using the context from the database for this reason I'm just going to take a variable call it item that's going to access this from the database by using the underline context variable do items to access the instance of the item created in our database in in our context that we have here and then we need to write a method uh method to list all of these items just like this but in in such a case when we query data from the database it's recommended and it's always what we do is that we make the method the action method to be asynchronous an asynchronous action method basically allows us to await some data from the database which means that the method will stop until we take all of the necessary data that we need and then continues where we left off for this reason to write an asynchronous action method we need the async keyword here on the method and then this method needs to return a task of I action results and we need as well to write the await keyw in front of the part where we query data from the database and in a case like this we need to use as well the to list async method which lists these items asynchronously we need to import Entity framework ore in order that it recognizes the to list a sync method Entity framework core now we do not get an error here so basically what we did here our as synchronous action method allows us to wait until until this task is fully performed and then it continues to return the view and the view should contain all of the data that we take here from the database now after writing the index method we need to create the view page to create a view page let's just go to the items folder which is the name of the controller and here I'm going to create a new view page with the name of the action index. CSH HTML let's remove this part here now in this few page use eraser syntax to basically embed the C code in our views as well and on top of the page because we in this page we are going to display a list of item models we need to use the at directive in order to import a list of these models to access them I'm just going to need to write the name of the application models do the name of the model we could also just import this directory on top of the page but just to write everything in one single line that's why I'm using it this way and in this code block here we usually specify the title of our page the title of our tab basically we do this with a view data which is a dictionary in C this works with key value Pairs and we can set the value of the title by just writing view data title here as the key and then we input the value form of a string let's say index here for the index view page if you actually go to the layout page where we specify what will be displayed in the title of our page you can see here that we can give a value to this view data to this dictionary with a key of title and we automatically give it this text afterwards we can remove it we can leave it as it is so that on top of the page we'll see let's say Index dasy app the name of the project and this is how basically view data works now back to the index view page here we can give a header to our page now H3 header named index and we usually use a table to present a bunch of data that's the example I'm using in this case so like an HTML table element with a class of table and we'll separate it into sections table head for the head of our table this is just some HTML syntax and table body for the body of our table in the table head we can use this table row Texs to write everything we want to be displayed in the first row basically this table head and we can use a table head header here by writing th H and here we can inut the name and the price of the table the price of the item if I just turn the project you should so that you see what we're doing at this moment just click with the right of the mouse here in the view page you want to be displayed and then click viewing browser so here we have specified the table header only the name and the price of the table and then the data will come down below in the table body so the table body here we can use a for each Loop this will be a c code and the isor pages allow us to use C conditionals in it by just writing the add sign and then the for each keyword here we'll take a variable and give it a name like item but it doesn't matter the name of the of it in model so this keyword is important so in a variable we create just create a temporary variable here and we are looping through the model that we are inputting here on top of the page and down below inside an entire single table arow we will display by using the table data tags the name of each item by writing the at item which is the name of the variable we specify here that name and another table data for the item. price and so the price is basically formatted as it should be we can use this two string method and inside parenthesis the N2 this is just a method that basically displays the the price which is a double with only two places after the comma the last table in the last row here I'm using another table data tag to have two links to redirect us to the edit and to the delete page that we will create later on so we'll see a table with a list of items and by having two buttons there two links will then be redirected to an edit page for that item or to a delete page to delete that item this action methods that I'll redirect you right now will'll create later on but just that we have it there so I'm going to use this anchor tags to redirect us to an edit link and inside this anchor tag I'm going to use an tag helper an AS action tag Helper and I'm giving it a value of addit now what are TAG helpers tag helpers are this are part of the Eraser syntax and they make it possible for us to do specific tasks in this example this tag helper we redirect the link to the edit action in our controller that's the role of this tag helper there are TAG helpers for different things but basically this is the good thing about raser syntax that it allows us to connect our pages to our server side code or to our controllers I'm going to actually use another tag helper to specify the ID of this edit page by using the as- route D ID at item. ID so will redirect us to to the edit page for the specific with the specific ID for that item for that item and now we get errors here because we have not created that action yet right did I remove it here addit okay my mistake there here I'm going to use a tag another tag helper which works the same way but just will redirect us to the delete page for that specific item and this is all will display in our index page I'm just going to comment this out for now because we'll get many errors and just go back to the controller to proceed with the next action now the next action we're going to create will be the action to create an item firstly we just need to create a public I action result with the name of create and this will just return the view the corresponding view that we're going to create right now so just go to the to the items folder in our views folder and create a new razor page an empty eraser page here I'll name it create and just as we did in the last the last time we need to import the model not a list of models but just a model so the name of our application do models dot item and will set the typ title of the page with the view data dictionary here to be create let's just input a header saying like create an item and now here what we're going to do we're going to use a form to submit the data that will input in the form to another action that we're going to create so that basically we store this new item that will create in our database with the help of Entity framework cor so I'm using a form first of all here and I'm going to need to use a tag helper to connect this to an action in our controllers named create and we need to specify here the method to be post we are basically going to create a second create action in our dat in our controller which will be of post type what this means is that this action will be able to get only post requests so get requests are those request when the action returns just some data from the server or a web page and the actions that accept post requests they are actions that retrieve data for of form submission and inside this form here we're going to have some div tags for each of the properties that we will take in this form so let me just give a class to this div this is just a bootstrap class for some styling which is named form Group which you can see in the boot sh documentation to be make the form a bit better looking and I'm going to set a label for the data that we'll input here and we'll use this four attribute to connect connected to the name of the item basically here inside the label I'll just name name which is what will be displayed on the screen the text and underneath we'll write an input tag which will accept data of type text we give the class of form control to make it a bit visually better and with the as4 tag here we'll connect this input to the name property of the item so that when we input this data to our create action this will know that this data is the name property of an item and just like we just like we are receiving the name in this div group here create a new one to have a label for the price here and to also retrieve an number here in the input and this will be connected to the price attribute the last thing we need in the form is a button to submit the data which will have the type submit we'll give it a boot shap class to look visually better button button primary which gives it this blue color and just name this button submit and this is how our form should look like if I run the application and go to actually this page the form is not yet optional because this page is not yet functional because we haven't created the second action method but we'll just see how it looks like let me see what error I did here okay line four here I just misplaced the semicolon let me run the app again and go to the create page slash items slash create this is how our create page looks like we can inut a name here and the price for our item and if we hit on submit this data should be inputed to our action method and then where we will specify how we will store this data as an item in our database so let's just get back to the code and go to our controller so here here will specify a second create action and since this action will take post request we need to specify a top of the page HTTP post for get requests for get for actions that get that receive get requests this is not necessary to note there to to write there so I'm going to continue with the method the action method here which will also be an asynchronous method so I'm going to need to write the async keywords this will a task of my action results I'll name it create and here we need to use an attribute called bind to bind this what we receive in this action basically the ID the name and the price of the data that we inut in the form and to bind it to the item model so that the method recognizes that this data are part of an item here we need to write an if Clause to basically check if the model inputed in our form is correct so we're going to use a model state that is valid method so this method here of the model State class just verifies of the model inputed in our form is an actual item basically and if this is the case we'll use the context. items. add methods so this method that Entity framework course gives us an add to the context this item so basically the input from our form where adding it to the database in this line right here we are using the await keyword here and we'll just use the context one more time to save the changes done to database by using the save changes async method and after the changes are saved in the database we just need to return the viewer to the index page for this reason we're going to use the redirect to action that will redirect the user to the index page just like we return the view redirect to action basically is an other result type just like view result or Direction director action result is another result type that will redirect the user to the index page as already mentioned with big I here and if the model is not valid we'll just return the user to the current view so again to the create page and we'll input the item here let me just run the project and see how our create method action method is working I put a name for an item let's say microphone that price it like $200 or something just 200 since the price accepts a number and click submit should be red directly to the index page and here we see the name of the item and it's priced with two places after the comma and to basically I can just input a dollar sign there manually in the index page in the items folder so just here like a dollar sign after reload the page here and we should see the dollar sign after the price okay now let's just go back to our controllers and write write another action which is going to be the edit action this in this case we still going to need two edit actions one for the get request and one for the Post request for firstly let's start with the get with the action that accepts get requests this is going to be an asynchronous action or action method which will return a task of my action results result I'll name it edit and this action will actually receive an ID let's open up the curly Braes again and I'm going to use an item variable to look through the database as search for that for specific item so basically when we are redirected to a page with the details of a specific item with a specific ID we will look through the database to find that item and display it in the screen so I'll use thewait keyword here to await the result that we expect from the database so underline context. items that and here we'll use the first or default async method that will search for a specific item in the database where where the ID of that item will be equal to the ID that we take here in the action parameters and then I'm just going to return the view with that and the view will contain that specific item let's just create now another view page for the edit page I name it edit and what's interesting is that the edit view page will be similar close to identical with our create page because it will receive a form that can be changed from the users let me just copy what we had from the create page here put it in the edit view page the only thing that we going to change here is the title of the page which will be named edit the header I'll change it to be addit an item and I'm going to redirect the users that submit the form to the edit action in our controller the method is going to be a post method and as you can see here basically the user will be redirected to a form with already filled data form to change the name of or the price of that specific item and when a user changes the data here and submits for the data of the form it will be the user will be redirected to the edit action and we'll need to create the post action in our controller as well so need to write the HTTP post data annotation this will be a public method it will be a synchronous as well will return a task of I action result I'll name it edit and this action method will receive an ID which would be int and also the data from the form and we use this bind attribute here as well to bind the data that we receive the ID the name and the price that we'll receive from the form to the specific item model and again we need to make this check if the model inputed is a valid it's a valid model we need to use the context here and update the database with that specific item then we need to use the await keyword here and save the changes to the database with the save changes async method and then we'll redirect the user to the index page and use this redirect to action results and we'll redirect the users to the index page and if the model is not a correct model basically we return the user to the page already were together and we'll input this item to to the page so that when the user will still will land on the same place on the same edit page the form will be already filled with the data so let's just around the project here and test this in action let me check what I did what kind of mistake I did there think I forgot to close the method here the action method should be okay right now okay let me just refresh the application and I'm actually going to go to our index page the index page in the items folder I'm going to uncomment the addit Anchor tag here so I'm just going to comment this anchor tag that deletes the items so I'm just pressing control k+ C to comment this part and we should have the table we should have a basically a link to redirect us to an edit page for the item that we have already stored in our table so let's just refresh the page here here we have the item name the price and this addit anchor tag which is basically links us to another page the address of this link is items sledit SL1 which is the ID of this actual item and here if I would change the name here let's say change it to just like mic I can change the price as well if I hit on submit we should be redirected to the homepage and the name is changed the price is changed so basically when we submit the form we call the HTTP post edit action method the then we basically check if the model the data submitted is correct we update the database with that item that is inputed there we save the changes and we redirect the user to the index page now last but not least we'll need to create a couple of other action methods to delete the items the first method will be will accept get requests and it will be a public asynchronous method which will return a task I action result I'll name it delete and this will receive in the parameters here an INT ID I'm using a very able here I'll name it item and'll look through the database for that item with that specific ID so I'll use the await here to await the data from the database and then underline context. items that this first or default async method to search for that specific item the items ID that we are looking for will be should be equal to the ID that we are taking here here from the parameter and let's just return The View with that specific item variable that we have here let's just go to our items folder in our inside our views directory here and create one last view page I'll name it delete in this view page we need to import the item model as well so by just writing the directory of the models and the name of the specific item of the specific model we can specify the title of our page here with this view data we'll name it like just delete and let's just input a header here asking the user like are you sure you want to delete this item question mark question mark and let's just inut here in form of few paragraphs the name of that item that we are receiving in in this view page we'll access the item we writing at and the model attribute that The View page has that name and I'll use another paragraph to access the price of that model let's just write the dollar sign here as well and if the user is sure he wants to delete the model the item that is displayed on the screen we need to write a form for the user to be able to delete it so form the as action tag helber that we have been using so far to redirect the user to a delete action method that will create which we haven't done yet which will be of type post and here we're just going to need an input to take the ID of our model and because it's not a good practice to basically display on the screen IDs we just put this input to be of type hidden so that we won't see the input on the screen and by using the as4 tag helper we will connect the value in this input with the ID property and then other input we're going to need to be of type submit this is just like a submit button actually we'll give it a class but Shar class of button and button danger which makes the button red and the value or what will be displayed in this button or in this input will be delete now when the user clicks on this input time and submits the form he will be redirected to the delete post action method that we we will create right now set the data annotation of the method to be HTP post and the method will be public it will be an asynchronous method to return a task of I action result and this method will it would be good to be named delete as well but we cannot name it delete because we already have another delete method which in this case will contain contains the same parameters so when we have two action methods that have the same name and the same amount of parameters and the same name of parameters we cannot have these methods of with the same name for this reason I'm just going to name it delete confirmed as sub practice this will receive an INT ID parameter and actually because we specified in our delete page here in our form we specified that the form will be will be submitted to a delete action the practice common practice to be used in this case is that inside this data annotation here you can write the action name delete so whenever user sends a request to a to a delete action of type post this method here will receive this request now I'm going to need a variable here to get our specific item from the database I will await the results and we'll get the item from the context by writing context do items and I'm going to use defined async method here we'll inut the ID so we're finding the item we need with this specific ID that we get from a parameter here and here we need to make an write an if Clause to check if the item is actually not null so basically if we can find this specific item with that ID in our database we want to remove that item from the database using the remove method which will take the item as an argument here next we just need to save the changes to our database and as you can see all of these methods we are accessing by using the context and then we need to return the user to the index page so in this case in this action method here even when the item is null or not the user will be redirected to the homepage to the index page let me run the project one last time to check all the functionalities of our application and before I forget I'm just going to go to the index view page to uncomment the Anchor that will redirect us to the delete page just hit on control K plus u to undo the commenting here the ID here is apparently with a lowercase letter let me run again the project and let's just go to the slash items slash index page okay here we have our only item here the mic oper price with a price of $300 just actually going to create a new one slash items slash create let's say keyboard price1 $100 hit on submit we have our second item here we already sold the delete the edit option that works click on submit we'll be redirected to the index page let's try the delete how it works if I hit on delete here we'll go to the route items /d delete SL1 which is the ID of this specific item and we see here I sure you want to delete this item we see the name of the item and we should have seen the price here but apparently something is not right there okay so we need to specify here the model. price let's just refresh the page okay we see the mic the name of the item the price of the item if I click on delete we see that we do not have this item anymore in our index page so basically we called the action method the delete action method in our controller here we found that item in our database we removed it we saved it change this and redirected the user to the index page what is a onet toone relationship let's say we have a list of items stored in our database and we want to connect each item with a single serial number we also want that serial number to be assigned to only one item in this case they need to have a onetoone relationship this is what we will learn in this video and it's going to be super simple you can see that our item has only one idea name and a price I'm just going to start with by seeing here that our name property in this case has this kind of error the properties by default are declared as non-nullable and the compiler basically just wants to remind us that we haven't initialized the value for this property yet now in some cases we would want to like add the question mark after the property type the error would go away but in some cases if we would not want our property to be null for example or that we can skip this error we can just give it an initial value here like null with the exclamation mark and basically this gives the name property a temporary null value so just to remove this warning that the compiler gives us we give it a temporary value where telling the compiler that we do not want our property to be null but basically we will take care that we give it a value in the future I hope that's not complex and now back to our one to1 relationship model I'm going to need to create a new class in our models folder I will name this serial number and let's give it like a a ID property as you can see the integers and numbers usually don't take an error because they by default they can be null actually but if we we'll give the serial number a name as well and to escape this warning we'll give it an initial value Here and Now we just going to need this two properties for the serial number alone but now to connect it to our item with a one to one relationship we just need to connect it with an ID with the ID of the item I'll write it here Item ID and also we'll specify here that this will be connected to an item model and I'm going to need to specify on top of this item property the the foring key basically how we will connect this item this property to the actual item is that we're going to use the item id property so we are using this data annotation to basically connect the item with our Cil number using the item id SE foring key and here I'm actually going to use the question mark on top of both of these properties because we want to to make it easy for us that we do not have to connect the serial number with an item just as soon as we create this model we can give it a value later on we create firstly the model let's say we create the item and then afterwards we can connect it to a serial number and vice versa so I'm just going to go to the models folder one more time to the I'm going to go to the item model as well and I'm going to connect it to the serial number I'm going to need the uh serial number ID property and a serial number property named serial number so basically what we're seeing here is that we will connect this model with a serial number the item model and we're going to use the serial number ID as the forign key basically but we do not need to specify this data annotation in both of our models in one of them it would it's enough and I'm going to set this to be notable as well here so that as I also said before we do not have to specify the serial number ID just as soon as we create a new item model now that we have this clarified let's just go to the context I'm going to need a dbet instance for our s real number model serial number I'll name it serial numbers in plural for us to give some initial data to the an item and a serial number in our database we can use here the we're going to need to overwrite a method in that Entity framework or provides us so I'm just writing here protected override void since the method won't provide anything that on model creating and what this on model creating does here this is like a method that Entity framework or has which takes the model builder class as the parameter which is a class that will allow us to configure the different configurations for our models and the relationships with them so actually by default there exist an on model creating class Entity framework C that configures our items our models and the relationship between them by default but now we are overriding it to specify either the data for our models when they are initialized or the relationship between them so just inside this method here what we're going to do to I'm going to use the model builder parameter which was this class that we'll use to add configurations and I'm going to write that entity I'm going to write for the item first so I'm just going to use a method has data here just the model has data here to give some data for a model basically to create a model in our database for the item how we can do this is I'm just need to write here new item and I can let's say specify the ID to be like four it's a number I don't know if I have an ID with an ID of one in my database the ID is an integer so no double quotations are needed the name of our item I'll set it like a microphone I don't know some item and now I'm going to add the serial number ID do I have a price for the items I think I do okay I'll set the price to be don't know price to be $4 or something and the serial number ID which will be the ID of the serial number is I'll input like a number here like 10 and then I need to create a serial number with the ID of 10 so that these two models will be created just like I created an item in our database I'm going to create a serial number as well so I'm just going need to write here serial number we're creating a new serial number I will use the ID of 10 so that the first item will be connected to our serial number I'll name it like the name was a string I guess so I'll name it like ik like 150 or something just some code with just what could represent a serial number actually and now I think the only property left is the item id which I'll set it equal to four so that it will be connected to our first item after we have created two models in our database we have the database instances here for the models tables to be created in our database I'm just going to need to go to package manager console and add the migrations so add migration I'm just writing here one: one so that we know for this migration we addit the one to one relationship in our database you can name it whatever hit on enter okay now we just need to update this migrations this database schema that was created here here to the database everything seems to be done correctly now I'm so that we show this on the screen actually I'm just going to go to our context to our index page and when we load the items from our context we need to specify here that we want to access the serial numbers as well we just going to need to write an include method after we access after writing context. items after accessing items in our context text and after writing here include just going to need to get the variable here and access with it the serial number now that we can have access to the serial numbers from our items we can go to the index page and also display the serial number in a column so here like in the the table header the table header just right here serial number and we'll access it from the database here by just writing item do serial number. name of the serial number we can just r our application now and go to the index page let's go here to slash index slash items the name of the controller slash index now we are getting an error here while processing this in our index page and the error is actually happening because we have specified this serial number in the item that it could be null for us to avoid this we can just give a question mark after accessing the serial number here from the database and in case it is null nothing will be displayed in that column if not we'll access the name of the serial number let me just run the application one more time and here we should see a microphone priced 40 and the serial number is mic 150 so we have connected the microphone with a one to one relationship with a serial number and actually vice versa as well and if we would try to we cannot connect this serial number with another item actually even if we would try to do this either in the context or in our database this would give us an error and the first item actually here we haven't connected it to a serial number yet that's why this column is empty for the moment let's say we have a list of items stored in our database and we want each item to belong to a single category in this case one category can be assigned to many items but each item can belong to only one category this requires a one tomain relationship between the category and the items in this video we'll learn exactly how to set this up but don't worry it's not that complicated we'll connect our models to each other make the necessary changes in our action methods and then see how it looks on the front end so let's get back to the project and let's go back to the item model sry main model and we can actually connect this to like a category and a category is like a one to main relationship model with the item but from the categories side so a category let's say electronic books or something like that can have multiple items be part of it but a specific item can be part only of one category so how we can write this relationship let's just go to the models folder and add a new class let's name category and I will give it normally an in ID property a name for the name of of the category and I'll initialize it so that we skip the warning that the compiler gives us so giving it like an initial value and telling the compiler we'll set a value to this name later and then we can connect this we should connect this with a list of items I'll name it items and then make this property to be notable with the question mark so that when we firstly create a new category for example we do not need to like to connect it with an item right away we can add this later on so one category can have multiple items but if we go back to the items model one item can have only one category so I'm going to need a public ID property not int ID but let's say category ID and the public category named category let's specify the foreign key here so that our category will be connected our item will be connected with the category model with using the category ID property as a foreign key and I'm going to make both of these properties as nullable so that if we have created a new item we do not need to specify the category right away now that we have specified how this relationship looks in our model classes I'm going to go to the control to the context and add a dbet instance for us to be to be able to store this category model in our database as a model instance so I'll write here category name it categories in plural as a practice and I'm also I'm going to create manually two categories in our database so I'll use this model builder class as well entity. category that has data method and just write here a new category let's say an ID of one and the name of let's say firstly Electronics I'm just not going to connect this with an item right now we're going to set it up in our action method so that we can do it let's say interactively in our application and like a new category as well would be okay I give it the IDE of two and I'll name it let's say something else like books now I'm just going to add the migration firstly to the database and then we'll handle our action methods so just add um migration one to many let's name this migration and I'm just going to update the database okay the first thing I'm going to do is go to the items controller let's start with the index action method we're going to need to include the categories when we search for the items in our context so I'm just going to place this a bit below in one line so that we can see everything better so I'm just going to write this that include method again give it a variable like C and just include the category when when we search for the items in our database so that this is included as well but now that we haven't connected actually any of our items with any categories so we're going to need to set it up in our create page so that you know when you create like an object you will have an option let's say when you create the item you'll have the option to select one of the default categories for this reason we are going to need to display all of the categories that we have in our database in the create page how we can do this we're going to use the view data dictionary that we can use in asp.net core I'll name it here we'll give it a name here of like like category and here we'll give will'll give a select list to this view data what this select list will contain we'll look through the context to get the categories and we'll search them by the ID and we'll basically say here that we'll display them by their names so we'll get the ID and the name of these categories from opening it through the database and this list that will be created here will give it into our view data dictionary we're going to need to write this using directive on top of our page this using microsoft. hp. net. nbc. rendering which basically is the directory that includes this class the class for a select list and now we have basically have a view data dictionary passed to our create page that we can use in our create view page let's go to our create view page here and how we will access this I'm going to need another div here a form here for the the user to be able to input a category when they create the items so the label will be for the category ID let's say category here as the label for this form and actually we are going to need to use here like a select HTML tag here we need to specify by this tag helper as4 to connect this to the category ID property and the in the ASP item in the ASP items tag helper here we're going to need to include basically what we take from our view data dictionary we can do this by writing viewback do categories which was the name of our view dat data and the view bag is actually the class just like a dictionary that we use to take this data we have from our controller and display it here on The View pages so this is as well another class and I'm just going to name give a class to our select list here of form control and this is it with the view page what should we should write in our view page if we go back to the controller now if we go to the Post action method we need to bind here the category ID that we'll take from that select list as well and we could leave it as it is now but we can do the same thing and modify the edit page as well so that for the items where we haven't specified the category we can addit them later so I'm going to need this view data dictionary here to input in the edit method in the edit action method in the post method we can include then the category I did just the same way we did with our with our create method and we are going to need to modify the edit view Page by just giving it this line This div group here for the list that we had from the create page let's input it in the addit view page just the same thing so we're inputting like there like an form for us to modify the category as well and if I run the application let's just see how we can create an item with a category from the list of categories we have in our database and how we can edit it for maybe the items where we didn't inut a category so if I just refresh here let's go to the create page okay so we have probably made like a typing mistake somewhere this select text I forgot an e select here as well and let's just update it in the edit page as well okay let me run the application one more time here we see in the category input here we have a list of both of the categories that we have written in our database so we can select either Electronics or books since we have a books category let me write the name for a book let's say Tech book like c book let me give a price to it like $20 the categories books let me submit it it should be redirected to the index page c book was created here we had the price we didn't give it a serial number but we forgot to write the column for the categories actually so let me go back to the project to the index view page okay just another column header here for the category and down below as well to get the value of the category for each item so item do category do name we can put the question mark here if it's null we'll display it we'll display nothing let me run the application one last time at least I hope one last time okay so we see here in the category column here we see that for the item that we created we see the books the category now since we changed the edit action method and the edit view page we can basically edit these two other items to have a category as well so if I go back to the first item and click on edit we can input a category to the first item click submit and the keyboard now has as an Electronics category let's go to the microphone item I'll leave it as Electronics as well click on submit so we change the category for these two items as well so the electronics category is connected to two items in this case let's say we have clients and items in our database each client can have multiple items and each item can be bought by multiple clients this creates a many to many relationship that we need to manage with a helper model in this video we'll cover how to set up the models configure the relationships in the database and make the necessary adjustments in our application so let's just go back to our project and our main model was the item model now if we connect the item with a client model let's say one item can be purchased by multiple clients and one client May purchase multiple items as well so this is a many to many relationship between the item and the client unlike the other relationships we've seen before this type of relationship requires an additional helper model to connect these two models with one another which we'll see in a bit but firstly let's just create a client model by just going to the models folder and adding a class for the client and firstly we'll just give it an public in ID property and the string name property will initialize this property here to avoid the compiler warnings and now as I already said we're going to need a helper model so this this client model will be firstly connected to this helper model in order then to be connected to the item model we'll see this better after we create this helper model which usually takes the name takes a combination of the names of the two original models we can name this item client and this model will be connected to both the client and to the item for this reason we are going to need firstly a public int item id property to connect to the item and of course an item property named item so basically using the item id this item client model will be connected to an item model and the same thing will happen also for the client model so we need a client ID property and the client property named client so that basically what we are telling the item client here is that the item client model will be connected to the item and to the client using these fore in Keys respectively so the item ID for the item and the client ID for the client model and in each of the other two models we need to connect these two models with this helper model and basically we need to have here a list property a list of item clients to connect our model with multiple item clients I'll name it item clients as well and I'll make this property nullable so that we do not have to assign a client firstly as soon as we create the item or as soon as we can create the client then and I'm going to copy this property and paste it in our client model as well so basically the client here and the item as well are firstly connected with the item client which then connects them to the specific item in the case in this case for example in the case of the client and now what we need to do is go to our context class and actually add these dbet instances of the two new models we created so that they will be stored as tables in our database so public dbet client I name it clients and also a dbet instance for our helper model named item clients and unlike the other relationships actually we are going to need to specify this relationship of the item and the clients with the item clients model in our context as well so just somewhere inside this on model creating method we'll basically firstly use the model builder class to specify the relationship of the item client model or entity with the item and the client basically here we'll first specify the foreign keys that are in this in this model so item client we need to write here that has key so here we specify both of the foring keys let's take variable here item client we need to write the new keyword and inside curly braces then here we will specify the two foreign keys so the item ID and the client ID and in another line then we will basically specify the relationship tip that this item client model has with each of the two models so here we specified the keys firstly the foreign Keys here we specify the relationship in a bit more detail so we need to use the model builder class again that entity item client so we basically say here that the item client entity is connected it to the item model first the item model and we write the with manyu method that specifies that an item can be connected to multiple item clients model and we specify the forign key again which will be the item ID and we'll cop I'm just going to copy this line and do the same thing for the second relationship between the item client and the client model so we just need to change here the relationship so it's connected a client model with multiple which is connected with multiple item clients and the foreign key here will be the client ID and that was it for our configurations in our DB context about the relationship between our three models now let's go to package manager console let me make this bigger and add another my mtion for the many to many relationship I'll name it just many to many name of the migration and the database schema or the migration was created now let's just update the database so all of the changes we made in our project will be reflected in our database as well everything was built successfully so now for this video I'm going to go and see the database and we are going to populate the data in the database so I'm just going to go to the database that I connected with this application Let me refresh it if we go to tables okay I'm just first going to go to the items table and click on addit top 200 arrows so that we see the top 200 arrows okay let me just see the clients table addit top 200 arrows as well and the item clients model okay so what I'm going to do here let me just firstly create two clients one with a name let's say Zack and one with the name like Martin so we have two clients with idf1 and two let me just find our items here so our items we have an item with the ID of two one with the ID of four and one with the ID of 10 and2 a th000 and two actually so I'm going to connect in the items item clients table here I'm going to connect the item with the ID of I think two with Zack here which was a Cent with IDE of one and the item with IDE of four with Martin and the other item was had the IDE of 1,2 and I'll connect it with Zach as well so Zach here has purchased two items so to say okay now that we have set up the some data in our database let's go to the project and I'm going to need to go to the index action method in our controller actually so that we include here the when we query the items in our database we want it to basically include also the list of clients with which an item is connected to so here I'm going to add a new line and click on include I see goes to I see. item clients and then in this case in a man to relationship model we need then the then include method so basically after we include here the item client helper model which with which the item is connected then we need to include with this then include method the client client's model because the item first is connected to the helper model to the item clients model and then it is connected to the client's model I hope that's not too complicated to the client okay now I'm just going to need to go to the index page to the index view page and add a new table header here for the clients clients and down below where we display the list of clients with which a model is connected to I'm just going to use this table data tag here and I'm actually going to use a for each Loop because one item may have multiple multiple clients here we Loop through a variable I'll name it client in item do item clients with which is here this was the temporary variable we created and we look through each items item clients then we will'll display each the clients client. name so we are accessing for each item the item client's helper model and then the client. name property of that model let me just around the application let's refresh the items route we should see another column in our table and we see for each item we see the client so for the keyboard we see that the client is Zack for the microphone is Martin and for the book is Zack again so basically in this case each item has only one client but the client has two items and this is basically how the many to many relationship Works in as. netore MVC if this video was helpful YouTube thinks you should watch the video that's now appearing on the screen so I'll see you there don't forget to hit the like And subscribe buttons for more content like this and I see you soon