How to Use One to Many Relationships in ASP NET CORE 6 0. One-to-Many Relationship in EF Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to macro code and today we are going to learn about one-to-many relationship conversion in Entity framework so if you're new to this channel consider subscribing and watching our previous videos so this is the continuation of affluent API video so you can consider watching our previous video and subscribing so in our previous video we learned about uh EF core fluent API so we are going to learn about EF convention which map entities to different object objects of the database so today we learn about the relationship convention between two entity classes that result in one-to-many relationship between corresponding tables in the database so Entity framework follows the same Convention as Entity framework 6.x 4.1 that is a when a Min X is 6.0 going forward for one two minute relationship so the only difference is that efco creates a foreign key column with the same name as navigation property name and not as navigation property underscore primary key property so we have our we are a foreign key we have a one of our model called a student that we created on our previous video so we have these entity call students so so this student we have created ID first name middle name last name date of birth and the phone number so we are going to create another model called grid so we are going to link uh so I'll create another class so I'll stop my application get another class called grid so in our grade class you'll have of course the primary key inch then this is our primary key then in a grid table we'll be having our grid grid name String name then we'll be having our let's say section so after we have done this uh to apply convention formed meaning relationship in in the entities so we have our student model and our grade model so we want to establish the relationship in Entity Framework so so we want to establish I want to many relationship where many students are associated with one grid so this can be achieved by including a reference navigation property in the dependent entity so here the student entity the dependent entity and the great entity is the principle entity so our student entity is the dependent entity so we'll do this so we'll come here and say public then we say grid then you say great so this is our dependent entities here so we have uh establishing now the connection they want to many relationships so he as you can see after we have done this the student entity class includes a reference navigation property of of grid type so this allows us to link the same grip to many different students which creates a one-to-many relationship between them so this will produce a one-to-man relationship between the students and grades table in the database where the student table includes another warranty of grade ID so you can actually also do this you do that int then you say grid ID which will reference the grid so uh so we Define our our our table that is here on our students so we can add this to our grid so that we have this table and our database then we say grid so you can watch our previous videos on how to on application DB context that is uh defining the database connection between our application and our our database we are using SQL Server so if you are new to this you can check on that so after you have done that uh another thing that you can do on our grades on our grades table so we expect a collection of students so you can actually do these public Eye collection so this is another convention if you want to do it so you can actually Define that these and a student undergrade you expect students so we can also do we can do this public the Eye collection then we say student s yes so here the student entity includes the reference navigation property of grade and the grade entity includes the collection navigation property I collection student which results in a one-to-management between the corresponding tables the students and the grade so so we have done that so this is how you establish the relationship between one of the entities and the between the tables in the EF code so we have this and we have also have this so if we do migration so if you in case you may want these these uh Grid or the student can may not be linked to uh one of these table you can also have this to be nullable so you can do that then you will will actually be allowing nullable for that type of for a student can maybe linked or not to agreed so you can have that as an example so that's how you do it so if we actually do this if we now do migration so to do migration I'll just come to tools then again nugget manager then I say add migrations my location say add migration notification so that is the command for running migration so my migration is done then if I want to apply this to our database I'll say update a database then that is done that is done so on our app settings we have we had uh linked our database too by University then we have so if we I open my uh SQL Server you see we have our our database called University and the tables now we educated for students if I refresh this foreign entity we have the ID grid name and uh section then on our student we are expecting now the grid ID so if I run these uh select for students you see we have now a grade ID so assuming we want to create one of the student so I'll just come to our controller to our program.cs and you can add a crude operation for students so let me just do this so we'll come here and uh an application then you say add then you say and just come to our our views say add then you say new folded item then you say MVC with views using Entity Framework then select our class so we want to do crude operation for student this is our DB context then our controller name will be student controller then we add so it will create our views that is a crude operation for student where we can add update and delete students we can also create another code for grid so that we see how we are linking so it has created our our students a student uh crude operation so here we can see we have a student as one of our then this controller you can move it up here so you can also do another one for for grade uh I will say new folder item then I select uh grid then you'll have a great controller so it has created that so if I want to create a student I'll come here I want just to change these to students then I run this so it's running our application yeah you can see we have this table so if I just create here you'll see I will be adding a first name middle name then we have the grid ID so let me just minimize this for you to see so so you see we have the first name middle name and we'll be having a great ID so you have not created our grid so I'll just you can link our our grid so let's push this here press to create the grids so we can have our just to change this so that you can see our grade and copy this [Music] and we have a students here so here you can see seconds the students here you can copy also these and we provide the Great here you can see grids so we save these and we launch our application so we have a great CI we click this it will take us to our page for creating grid so it will create new so you can say grid is computer then session we say programming so if we create this is something wrong foreign [Music] yeah that is it uh let's see with CTR L yeah is some validation issue can actually connect this so you will create so let's do these just a simple app yes do that list you see it has created our program so if we go to students and we say get students you can see we have our grid [Music] okay we have our students so we'll be having our last name middle name last name then date of birth then we can have the phone number then you can see we have the grid ID so if we just click create it will actually create our students with now the ID entity let me also do that generators see some validations so let us command is au verification so what you want to see undergrade it is showing the ID so you can change this to show the the great name is technically again we'll go here and you can have this as the great name then you cannot do that so it will be showing the green name instead of the ID so if I create you can minimize these so you can have our first name middle name then a phone number and you see our grid does that drop down then we can create then you can see it has created our our student with a grid ID so if we come to our database and you select you will see our student then our upgrade so that is how you establish the relationship in Entity framework course so consider subscribing and watching our video see you in our next session foreign
Info
Channel: Macro Code
Views: 7,360
Rating: undefined out of 5
Keywords: C#, AmigosCode, Classes, .NET, .NETCORE, Sundeep, Saradhi, freecodeCamp, ProgrammingWithMosh, MikeDane, IamTimCorey, TeddySmith, FireShip, DaniKrossing, Stefan Mischook, ASP.NET CORE, Coder Foundry, Jose Montemagno, .NET 7, .NET 6, Visual Studio, Frameworks, Nick, Nickchapsas, Jake Wright, Keep On Coding, QuestPond, Comrades Flavour, Amigos, Developer Filip, TechWorld With Nana, Techworld, GotoConferences, ASP.NETCORE, BoostMyTool, Migrations, Code, Code-First, Code-FirstMigrations, One-Many, One to Many
Id: NSJ28mseky8
Channel Id: undefined
Length: 17min 12sec (1032 seconds)
Published: Sat Jan 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.