Choose Between Fluent API / Convention & Navigation Property in .NET 8 EFCore Relationship Mapping.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends and everyone welcome back to net  code help Channel I am Frederick and I'm happy   to have you here today in this video we are  going to talk about relationships in net 8   EF core relationship can also be established  between models in two ways first one is by   using conventions and navigation properties and  that is a default one and the other one is by   using fluent API in this video we are going  to cover these two guys here so stay tuned and watch it out before we Dive Right into this if  you know you have not subscribed to this channel   then um click on the notification Bell as well as  soon as you subscribe to receive update as soon as   I upload new video you can also like this video to  support this channel grow as well so first of all   let's talk about establish EF cor relationship  using um conventions and navigation properties   so navigation properties allow you to navigate  and manage relationship in both directions by   using data annotations so for basic relationships  conventions and navigation properties are often   sufficient now with this it can be used in one  to one one to many but not many to many for now   EF C does not not give direct access to many to  many relationship so when using conventions and   ofation properties to establish relationship in  E call you can do that with one to one and one   to many or many to one relationships I believe as  soon as I I mentioned this relationship you know   remember the types of relationship that we have  isn't it we have thir types of relationship and   that is what we're going to have a look in here I  have a diagram here here that shows you the types   of relationship that we're going to talk about  in EF cor we have one 12 one so in one 12 one we   having a car company and a car model okay so  within the one to one one is a principal and   one is a dependent so one is a parent and one is a  child so which one do you want to make a reference   to the other one car company it is a parent car  model it is a child why because car company do you   provide or do you make the car model you see so  car company can exist but car model cannot exist   without car company I believe this is clear now  this also tells you that one company one car model   so two companies canot have the same car model you  see so one company one car model and that's all   about one to one relationship so you're going to  have a practical look with this using the same car   company and on the car model when we come to the  one to many or many to one one doctor one patient   isn't it that's one to one here talking about one  to many so one doctor and how many patients as   you can see from here when you go to hospitals or  clinics you see one doctor can attend to multiple   patient isn't it good so that's why we say it is  one to many or from this angle it is many to one   we also going to have a look on how to establish  this relationship using um navigation property and   conventions now the last one here it is a many to  many so think of it you in school and now we have   um students in here we have subject you see that  you have English we have mathematics we have other   subjects that science and other subject now many  student can also offer the same subject so many   student many subject so let's say it's a science  class in a science class we have our 30 students   in there they are offering the same science you  see so that subject can be offered by 30 students   and 30 students can also offer the same subject  aside from that we have other subjects um that   can be offered by many students you see and many  student can also offer the same subject so here   we're going to say that it is many to many we're  going to have a look on how to implement um the   following relationships and the first two here  one to one one to minut is going to be covered   by conventions and navigation properties and now  since the third one there's no um the EF C does   not support it and doesn't give a direct access  to this many to many we're going to use the fluent   API so when we done with this you're going to have  a look on the fluent API how to establish it one   to one one to many and how many to many so let's  say you having a simple application whereby you do   not need any large or any complex um relationship  then you can go in for the navigation properties   and conventions okay so that's the reason why I'm  going to introduce you to that first then we come   to when having a complex um app that you need a  complex relationship you can also Implement using   the fluent API all right so I believe this  is clear isn't it that's fine so launch of c and now that's 2022 let's create a new  project for this so since we are going to   be working with database we need to go in for  web API and this is going to be demo EF core relationships so the sour code of this project  will be available so you can check the video   description as well so EF call relationships  let's click on create to get this project created so we need to install three packages here so we  can be using it for our test purpose so let's   get them installed let's go to dependencies  right click and let's install EF core tools and SQL Server so we need entity freeor call so let's install this you want to  okay so we inst Sol the NTI framework call so we need to install SQL server and On Tools and the last one is SQL Server so we need to  install Okay so tools we need to install this to all right so we have them installed and  that is it okay so we can close this up if   you want to verify save this can just go to  solution then from dependencies you can just   check packages and see the three packages that  we installed recently okay we can build the project so let's first talk about one to  one relationship now we're going to create   a model for that so let's see so solution  and I'm going to create a new folder in here and these are going to be models now inside this model  we're going to create a class for this and this is going to be  the company and car model so   car company and car model all right so we have car company then we going to have car model so  the car company let's have our property in here so default you're going to have an ID for a  primary key we can also specify let's say this is   string for car name so for the name of the company  and now let's go in there and create the car model so with the car mod we going to have  the same ID for primary key then we going to   have string as a model okay now let's establish  the relationship so the car model and the car   company which one do you think is the parent  company oh it's a parent model I believe you   saying that it is a car company right because  a car company has to produce a car Motel so car   company can exist but car model cannot exist  without car company you see so in order to   establish this we going to make it in a straight  form that you cannot assign you cannot add data   to the car model without specifying the car  company as a parent company you see but you can   add car company say is a parent you can add car  company without adding in car model and that is   a one to one relationship so let's see how do  you stet this so we down here let's have this relationship and on this type here it is one to one yes okay now we have this so let's come  down here and establish the relationship   so in order to establish relationship here  this is a chart model so we can have access   to then car company okay and now the car  company we going to make here a same car company so with this we call this as navigation properties so this is navigation property  if you want to navigate to The Car Company   unless you can use this as navigation  property aside from doing this you need   to specify a foreign key okay so here  we're going to say this is int and Car Company ID it tells you that before you provide  access a data an object to this one the values to   each of the properties here you must also  provide a property to this this car Motel   cannot exist when the car company doesn't exist  so the car company should be there before you   can add car model take note the car company  should be there before you can add car model   because of the relationship here that we have  initialized this is a foreign key so FK now   let's go to the car company this is a parent  company so this this this can exist when car   model doesn't even exist so in to do that we  do not need to specify a foreign key what we   need to specify here is only the navigation  property okay so here we specify this is um relationship now this is one  2 one and I we need the car model and that is car model  and this is just navigation property okay so the reason is you have to also  make this as nullable why am I saying that else   when you are adding values to this you have to  supply value to this as well because as soon   as you decide not to add this now it TS it as a  required property so you need to provide values to   this meanwhile we don't want to do that we don't  want to provide values to this C Mod when adding   cap company so make it as snable the same thing  applies to this car model you have to make this   also aable so you can skip this and assign only  the foreign key okay so defaultly uh with the EF   call it's going to convert it or has a convention  that if you provide this as a foreign key and you   provide this as a navigation property it's going  to generate a foreign key with this against the   car company so if you want to add car model the  value that you apply for the car company or you   assign to the car company is going to check if  it correspond to any of the property or any of   the values or the record in here if not you're  going to throw an exception okay good aside from   that here we are not specifying Cascade property  that is the constraint the constraint could be   that if I delete you know if I delete the parent  the child might also get deleted defaultly it is   assigned to Cascade uh is it uh it has a Cascade  so it tells you that if I delete um car company   here eight correspondent to the car mod also get  deleted what if in case you do want to do that   if I delete car company I want to maintain  the car model that is when we have to use   a fluent API to customize our own way look at  that later on but for now let's maintain this   so this is the basic one and with this as soon  as I delete the parent the child would also get   deleted put that in mind when having a small  application you don't need to be using the   fluence API you can use this and I go your way  because it's very simple and easy to implement   as well isn't it that is it very simple okay so  we have this we going to create a connection so   let's see from here we go to our controller  no controller first we go to our upset let's   establish our DB Connection in here so we have  our connection strings then let's say this is   default so let's specify the server to be local  database is what EF DB trans connection to true   and our trust server certificate to also through  okay now once you're done with this we going to   the going to create our DB context right let's  create a folder here and I'll name it as data now   inside this data folder we're going to create  a class and that's going to be the DB contest class so appdb contest class this going  to be inside that is going toh from DB contest and now we can create construct for this so here we need to specify our  table so it is a DB set and here is car company and now we can talk about car companies we can also talk about car model and that will be car modos okay so we have our tables um created now   let's see let's register this in the  program so here we're going to have Builder do services. add DB contest then we need  an app DB contest class in here we can specify   options options. use SQL Server then let's have  Builder dot we going to have access configuration.   get connection strings then in that we're going  to pass in the default connection string that   we created okay so we did that here when you  check this AB contest that's the up settings   we have this so that's what we are providing  okay this is done let's save this let's click   on this set this to foral save this now we are  going to build this project again and perform migration now this is done so we go  to package manager console and now   in here you're going to say add migration that is first and now let's update the database you see that when you check here we have  car company created and I can see we have   primary key over here as an ID the same  thing applies to this we have a primary   key here but aside from that you see from the  car models we are establishing a relationship   in here so see we have a foreign key in here  you see and that is what we are having there   a foreign key that we created okay so now  let's say let's apply or let's update this database so while this is going to run we can  check from SQL Server object you can find it   from The View then in here we can open this you  open our desktop that's our server database and   see we have EF DB so if I open this we're going  to have on our tables um folder we're going to   have two tables in here for us car companies  and car model if I WR click on this and I go   to view data click on model and also go to view  data let's check it out so you can see from here   that we have Car Company ID so if you add or  before you add car model you must provide Car   Company ID let's check for car companies it just  ID and our name so you have to add this first as   a parent before you can provide value to the  Chart because when I'm providing value to the   Chart it's going to you required of a parent ID I  believe this is clear yeah so once you understand   this you can now implement it with your own  models um and keep going yes that's fine okay   now we want to create simple controller so we  can add data to this and also get data okay to   make this where complete so let's quickly go to  the solution and in that let's right click here   we're going to create class maybe let's create  folder in here and I'm going to name this as rppo so inside this repo I'm going to  create a class in here let's say this is repository in that I'm going to  create Constructor let's inject appdb contest let's create an assigned field and here  so this is public async task so this is ADD company so add car company and now this is car company okay so in here we're going to  say that appdb contest do car companies.   add then car company aside from that  a wait appdb contest. save changes   async so this is what we are doing  okay then we can also add car model so add car model and now here  we're going to say this is car model so from the car model we just  have to copy this add it here copy   this add it here and add s okay so we  are done now let's get C mod let's get all so we can have access to the same  in here let's say so public isent task then we can have um get car companies now this map up to await appdb  contest. car companies dot to list as sync okay now you can do say same to the car modos and now instead of having this car  company car modos and that is it okay let's   create a controller for this quickly we go to  controller now we can even change this weather forecast and make this as this is test okay so test controller and  now maybe you can specify an API   slash now I'm going to remove everything from here okay now let's create  Constructor inject the repository in here we create an assign fi for  this then here we can have access to post method and now this post here we say it is company so this is public asent  task and we're going to use I action result and so here we're going to add add  car company and I will need this car company model so in here we say this  is V result and equal to await repository. add car company okay okay so here since it's  going to return it's void type we have   to write something like this  and I return okay so we say saved so company saved we can also  copy this and I'll make a duplicate   here and I'll change it to car models  isn't it so this is mudo and add C model very simple one and we can just car  model and copy this put it here and now   instead of saying add car company what I going  to do yes do the right thing isn't it and now   you see model save now let's get all so this  is HTTP get and now with this we say it is company now with this company we are still   going to have same in here can  grab this P it here so this is get get car companies okay and now with this we can have return okay so I wait repository. get companies then we can also um do  same here so this is returning car companies so let's see the  reason why cannot convert this   to vo let's check Theory so get  company so this must be a tax of list list of car company and I we do same in here list of car modos okay all right let's see we  must close this and close this okay I think it's solved let's go  back again to the controller and now   from here we have it done so we can  use this Lambda expression to hand this okay that is all and we can make a duplicate here and this car mod so do get car modos that is all so let's find   this and but before that we have to  register the service that we created so builder. services. add scop then we say repository that is all let's start this out yeah  so it is ready now if I open it wiely you see we   have the post and I'll get now let's try to  push first so let's try with the model this   is a chart uh model so we you can see we have  a reference that we have to make but we are not   going to since we made this as null we can skipe  this but we need to provide company ID let's say   we do not and click on execute let's see the the  feedback that you're going to have from here you   can see that an error OCC while saving the ENT  changes see the in exception for details right   and when you check it you can see it tells  you that the inser statement conflicted with   the foreign key constraint that is a car model  slw company model and now saying that there's   a table company models and you need a column  ID we you need to provide value for it so you   cannot accept or pass in while you have not  created the parent component so let's create   a parent component in here so we're going to  say car company we made this also null so we   can clear all this and let's say this is Toyota so  for Toyota we can add remove this trailing comma   let's execute this and I can see this is saved  when you go to get company let's check it out   so get company try this out now you can see we  have the Toyota in here right now you're going   to access you're going to add the model and  that's going to be the parent not the child   so from here let's specify the idea of one since  we just added this one and now which model we can   have um vit okay so Toyota vit is a model now I  click on execute and I can see this is saved so   if I click on get model and then try this out you  can see now we have company ID as one isn't it now   we have this added we're going to add another one  so the same company let's put another company in here so we're going to have  this kantanka now let's add this execute then within this kantanka we can add this so we have the type in here and now you see  we are Bruna we passing in the same company ID   right now check it out we say Company ID is one  we've added it already and we are still assigning   to this one so we are assigning one perent to two  children isn't it okay now here is it possible   let's try this and check it out so click on  execute and now let's see we have an error   here why because it must be unique so as soon as  you assign one since it is one to one it tells   you that a p parent can be assigned once you see  that and now if you assign the parent you cannot   assign it anymore it is one to one so we have to  want to check the parent let's get our parent from here let's get our parent so you see we  have kantanka so you must provide ID of   two so let's go in there and provide I two  and now that be be all so execute and I can   see this is saved you see that it is sa from  here now when you try to get all models let's see so get all model you can see now we have vit  we have abona and this is from kantanka and this   is from Toyota so that's a one to one so one to  one one must be a parent one must be a child the   child should or the child is independent the  child depends on the parent before you pass   or you store data to the child you must provide  the parent ID you cannot use parent ID twice it   is only one that only want that is why you said  do you want to make it one to one relationship   that's fine let's go to one to many or many to  one relationship so let's close this one and   now the same repository that we created the  same model we're going to use it as well in   here so we're going to follow the diagram that  I displayed I check this you see we have one to   many going to use doctor and our patient so let's  quickly create that model and we're also going to   do same in here so we create our models in here  then let's create a new class this is going to be doctor then the doctor is going to have an ID I   believe you know this doctor  also going to have name so string okay now let's create the patient so let's add same in here or  we can just go like this straight D   doctor in here copy and I'll paste it  up and let's change this to patient okay so we have a patient class now  this patient I need to change this   now patient has an ID and our name we're  going to establish one to many so one   doctor many patient or many patient one  doctor isn't it so here let's start from   the doctor this is the parent so you're  going to say one to many so you're going   to provide list so you can use list or our  collections or virtual so let's use list of patient okay then here we can say um we need  patient in here let's make this as null so we   did not provide values to this patient when  assigned of providing doctor is a parent so   from the parent um model when you create  a table for it and we are adding data we   do not want to assign or have populate a list  of patients no we don't want to do that okay   what we're interested here is just add the  doctor name now let's go to so this one too many now this is one two many and that tells you  one doctor many patients okay so one doctor many patient now let's go to the  patient and in here we need to provide doctor make itable and we say this  is Doctor and we need to provide an ID so   this is Doctor ID so doctor is going  to take all of this you need provide   an ID so this is navigation property you  know this already isn't it and there's a   foreign key when you go to doct you have  a navigation property that's fine so we   have this in here and that is relationship  so it is this is mini to one it tells you many many patients one doctor so many patients  one doctor and that doctor must have an ID in   here so we cannot pass in patient without  specifying doctor ID but we can pass in   doctor while specifying the patient you can also  assign this doctor to many patients because we   are saying many patient in here correspond to  one doctor so the same patient the same doctor   can be assigned to many patient not like the  one to one whereby one doctor one patient one   doctor one patient no you don't do that okay so  once we're done with this we're going to create   our let's see from my repo so what I'm going to  do here is quickly we can just make a copy of this and I'll edit this quickly so I'm  going to pause this I'm going to do this   and I'll come back I believe you can also  do this right so let's give ourself about   a minute and I'll quickly work on this  so this is Doctor so this is um one to many so relationship and now here this  is one to one it starts from this okay all right so I'm done but you see we  have some red lines in here can you guess   why we having this yes because you have not  registered or created a database table for   this so quickly we have to go in there  and undo this magic let's save this we   go to the appdb context class and now in  here we're going to make a copy of this   then this going to be doctor and this is  going to be doctors you're going to have patient and this is going to be patient  yes so doctor doctors we have to move   this up yes so that is it we have  this created so when it go to the   rep and check it again you can see that  issue solved okay let's save this let's   apply migration again so this is not  going to be first this is going to be second so the file has been created as you can  see from here and we're going to update the datab base so you can see it is done now  we can um go to the controller and   then create a controller for this so  let's go to the test and now in here   here we're going to create the same  controller for this so let's first add and this is one to many post so we are  adding doctor and now this doctor add doctor to this and here we need a doctor model  and Doctor instance so model and our instance so here we going to look up for repy do  add doctor then we pass the doctor here so here   we're going to say doctor saved okay we can  just make a copy of this and I'll change it to Patient so so add patient and we need a patient model and a  patient instance now from here add patient   you pass in the patient and here patient  saved okay now let's go to get all so get   all doctors and and get all patient  so from here get all so this is duct test so get duct test and now we going to look up for okay so here it is get doct test and now the  same thing applies to the patient so get patient   we can have access to this method get patient  and now it is also get patient and now here   there's going to be patient isn't it so let's  specify as patient and that is all let's save   this let's run this and check it out okay so  let's see now we have our doctor we have our   patient so we can add you cannot add patient  without adding doctor check it out so here we   have this a doctor we made it null so we can  remove this then we have this patient so let's   say patient here it is red okay and that Dr ID  zero try to add this and you're going to have same issue let's see yeah so you can see when to  check it we have the same thing as we saw it   we need to provide um column ID as you can see  forign key constraint okay now let's close this   we go to doctor and with the doctor we can  clear the patient list off since we made it   as now and a doctor here it is freri okay so we  can clear this tring comma click on execute now   this doctor it is saved let's add another one  so here we have a net code H another doctor   that you never know until now so we have two  doctors in here right now let's go to patient   and we going to provide a patient in here so  this is patient one patient one we're going   to assign to drct one okay now saved now we're  going to have patient two and the same doctor   one so this has to work because it's one to many  so one doctor can have handle many What patients   execute this and you see it is saved patient  three doctor one check it out saved now let's   have patient for Doctor 2 doctor two and it's  also saved now let's get our patient and I'll   check it out so you can see from here we have  Dr id1 Dr id2 Dr ID3 right but see why are we   having this n n here it means we did not include  we can actually include this okay so if you want   to include all let's go to the same page and now  where we have our repository there our repository   so in here where we getting the list you see  from here do two list after adding doctor we   have to dot here if you want to include patient  let's see if this one will work so we're going to include patient okay then when you come to the  patient you're going to include dror test so   let's grab this from here patient do include  then this is going to be do test okay so let   me make it as D conductor does not stand  from start with P okay so we have this   here right and now we can actually handle  this now let's save this and let's refresh   it again and check it out so we're going  for patient try this out execute this and   boom we have this s because why you having  this because we have to include let's go to   Patient doctors and now from the chart you have  a list in here from the patient you have a list   in here so when you go to doct test once  you have this list let's apply this Json ignore they ignore that this guy save this and I  problem solved let's get patient again try this   out now you can see we have ID patient ID one  for you see we the same doctor been assigned   to patient ID of two patient ID of three patient  ID of four our patient ID of five has a different   what doctor that's next CoD out so you see that  here we able to implement one to many one doctor   many patient and doctor can exist but patient  cannot exist because patient is a dependent to   a doctor and doctor is a parent or the principal  in here good now that is it for using this   conventions and and navigation properties okay  from the third one you can actually make this   work but you cannot add data to it because EF  C does not provide any direct access you many   to many how do you implement many to many let's  give give a try and I and see how to implement   this many to many here and I'll verify or testify  to what I'm talking about so with the many to   many relationship you're going to use student and  our subject so let's go to Let's create our model quickly and now it our model right click now let's  add subjects so let's add student first because   student it is a parent now students going to have  an ID as you know ID and St going to have string name you see now let's create a subject so add a class here and this is subject and you know sub going to have the same thing ID then subject is going  to have name now let's create   a relationship let's start from  the parent now this is many to many so from many to many we're going  to have access to we can use list of subjects then we call this as what subjects let's  make this as null so we you do not provide values   don't forget this you're going to have an error  in it make this as no now let's go back to Let's   copy this or we can go back to the subject and I  provide list of students so instead of having the   same referencing itself we're going to have  student you see so one student there's no no   one student many students many subject okay so  many subject many students now this all that you   need to do when working with conventions and  navigation properties that's what I want to   do okay these are the navigation properties and  Convention is going to combine this you're going   to create a virtual table it's going to combine  the student and the the two tables together and   I create a separate table let's see but when you  create this separate table for now EF C does not   provide any API or any access direct access  to this table so you can have access to this   table but when you have table data in it you  can retrieve it okay now let's see how to work   with this so we're going to save this let's stop  this now let's apply database migration so this is relationship that's me to many let's go  to package me console and that's say St one okay so so we have it in here now  let's see okay we did not specify any   table in here so let's go students  we need to specify that inside abdb context so DB set and now we need what student our student then we need another one so DB set as um subject then this is subject let's save  this now let's perform the migration again   so instead of this third I'm going to  make this as four because we have third already so when you check this you see  we have a table we created and we have   the student table check it out that's the first  table we have subject table and we have student   subject table so this table combines the two  primary Keys as composite Keys you know and   these are the composite Keys you see have  the student ID and now the the subject ID   so it is taking all the two keys in there  two primary keys and I'll assign it to a   table and that makes it as what composite  Keys composite key is when you have a two   primary keys more than two primary keys in  the same table so we have the two primary   keys in here and we have the table name  as what student subject but with this we   cannot have access to this directly let's see  so let's update this update database now let's see okay so the table is done when you go to this  reference the table and you can see from here if   I you can see we have student subject right if  I click here and I click on view data we have   student subject and this needs an ID so student  ID and the subject ID now let's go in there and   ask subject and ask student let's see so we go  to the repository so there's the rep Tre where   is it are you found yours oh this is missing me  okay that is this one so here we have many to many now with this many to many you know what  I'm going to copy this because the same thing   so I can just grab this from here and I'll paste  it down there very simple yeah so uh student and   what I'm looking for here it is student so passing  student student let's add this student and that   is all we go to um guest student so guest student  and here we saying that get a list of student and student student okay by here do include  what we going to include P dot we can   include subject so when you do that it tells  you that we have to go to the subject we have   this list list we have to make this as  ignore Jon this is the chart so ignore Jason so Jason ignore let's  switch on top here let's go   to the reper again and um this is  get student so here after get get subject so subject and this is get subject From Here and Now subject okay so subject  do include students okay now add subject and another subject subject so subject.  add subject that is it let's create a controller   for this one so you go to the test controller and  now there's many too many so we're going to copy this and now in here there's Min many to many okay so here this is  student and as students so we know this already I'm going to copy this one here  and now change it there straight ahead so accident then student saved so here this is gu student so get students in here and now  that is get students and the method name   is what guest student very simple now let's go to subject so subjects and that is ADD subject now with this the mod the payload  here is going to be subject subject and   here we say as subject and I get subject  save so here we going to say so add sub   objects we have to provide this ad  okay and now the last one here it is subject this one subject and now get subjects  and now get subjects okay very simple let's   also run this and check it out so we  have the table ready now let's check   it out from the subject subject we're going  to add students so we can ask student from   here so the first student is let's code H now  we have to clear this off then let's click on execute that is saved so student one we have  net code put itself as another one we have Fred then we have the Frederick okay so we  have this yeah so we have this when you go   to get all students so gu student you can  see that we having this student but we are   having a list of what subjects you see so  this is null all we included subject but   this is not because this has to retrieve  from the subject table but the question is   how do we asset or how do we insert values  to this subject that is a problem you see   let's try with the next one the post  the subject now here you know this is mathematics that's a subject one so  it is saved let's add another one sign then maybe of learning English language  is very important to so let's say English then you know we have so we have three  subject in here let's go and get it out so get   all subject in Here and Now check it out we  also we also having the subjects right we   have the subject ID have the subject science and  we have this in here here so when you go to the   students you know we have a students but we do  not have the subject that each has when you go   back to the repo in here let's see from the repo  where is the repo that is one you know from the   student we are including subject isn't it but  it is not able to get it why here when you go   to the one to many we included the patient and  then we got it you're including subject and we   not getting it because of that third table we  do not have direct access to that third table   okay so let's maybe defaultly let's create  or let's open that two combined table and   our student ID one has a subject ID one the  same ID one again has subject ID one okay so let's see we have change  it to id2 has subject ID 1   okay now student ID 1 has subject  ID 2 student ID 2 has subject ID one they have are subject ID 3 now let's save  this one so these are subject IDs when you go   and find the subject you know the subject  we have the English and now the science   that we added right we save this and I  want run this let's check it again from here after saving this we can now run this again  and I you see now we have this so um student ID   one net code app has the following subject  mathematics and science now student id2 has   mathematics and English student ID three has none  you see so you can see here it is many to many so   many subject can be assigned to student and  at the same many subject can also assigned to   another student so many subject can be assigned  to many students and now many student can also   assigned to many subjects okay so one subject  can be assigned to multiple people and now   multiple people can also be assigned to the same  subject and other subject too and that example   what we are seeing in here so in this what you  to do here is combine the state the student and   the subject table together and this happening this  convention is done by the EF core and EF core does   not provide any direct asset the question is how  do you asset or how do you insert value to it that   becomes a problem so when you use these navigation  properties unless you manually add it in yourself   to database as what I did but I'm not sure this  is the best way to handle it isn't it that's the   reason why we have the flu API fluent API gives  you the direct access so you can actually modify   or create your own table specify your um your IDs  and now your relationships or your rules or your   constraints and Etc okay we're going to talk about  that in the next video so watch out for the next   video to talk about how to implement this um well  or detailedly in using the API in relationship we   are going to talk about mastering relationship in  net8 Entity framework call using fluent API I've   made a video explaining how to map models using  um conventions and navigation properties that   is a default one if you want to advance it that  is where you need to use the fluent API and that   is what you're going to have a look in this video  okay so one you say fluent API what actually is it   flent API is a way of configuring domain classes  and relationship in Entity framework core using   the model builder class and Method chaining  fluent API can override a default conventions   and data notation and provide more flexibility  and control over the database schema so if you   actually watch the previous video you will see  that we use the default convention navigation   properties to establish the model relationship  this time around if you want to actually have   a full control over your um relationship that is  where you need to use the fluent API let's go to   what we did in the previous video so you can see  when we check in our model we um created model to   establish relationship that is a one to one one to  many and a many to many so from car company and a   mod company we have one to one and as you can see  that is the navigation properties that we did and   now based on this there's going to be a convention  by the EF call when you run migration so we have   one to one and that is this one when you go  to the doctor and patient we have one to many   one doctor and a many patient so we have a list  of patients here in the doctor model and I want   you check the patient we have made a reference  that navigation property and I'm fing key in the   patient model and now for student and subjects  we made a relationship that is a many to many   relationship many students can have many subjects  many subjects can also have many students okay   and that is what we did and with the give getting  access to the m many relationship whereby it does   combine the student subject and now the students  because you know that's where we having this uh   many to many relationship between the student  and the subject EF C does not give direct access   access to the combined table so you know when you  create many to many relationship it's going to   pick the class the two classes and I'll add them  together and I'll create a virtual table for you   now this virtual table you cannot have access  to directly because EF C does not provide any   end point or any way to have access to it that is  the reason why we need to use the fluent API also   defly when you establish any relationship the  constraint here is in the CATE form and it it   it it means that anytime you delete a parent um  record it correspondent chart record must also   be deleted okay that is a default convention  done by the EF core but in this case if you   want to override it that is where you also need  to use your fluent API so that's why I saider   that when you use a fluent API you can have a  control a full access to your mod Builder and   you can manage your um relationship well within  your models and that's what we're going to have   a look in this video okay so we are going to use  the same model that we created in the previous   video to also handle this flent API relationship  okay so first of all let's tackle the one to one   now from the one to one company and the model in  the one to one you know that the company here it   is a parent or the principal and now the Moto  here it is a depend dependant or the chart so   that's why we're making a reference in here  and now the reference that we made here looks   like or it is like you cannot you cannot take  note you cannot add model car model Properties   or record to card model without specifying car  company because this the car model depends on   the company so that's the reason why we assign a  foreign key to this now after doing this we need   to configure this using the FL um API now from the  API we can do that inside the appdb contest class   so you have to go to appdb contest class and now  within this we're going to overwrite a method in   here and that exactly what we're going to use to  customize this so type in this overwrite and you   can see we have on model creating so this is the  method that you want to overwrite this is one 12 one and our one to one relationship is about  car company as the parent so let's say this is   a parent and now we have the car model car  model as the chart or dependant okay so we   going to configure this in here so within the  the relationship which one do you want to start   we have to start with the parent isn't it because  the parent first before the child and the parent   here is a car company so we start from the so  this is what we need to do to um configure this   one to one relationship a m model builder has an  entity called car company car company has one car   model and car model has one car company that is  a one to one relationship car company has um a   property in a car modor called Car Company ID  and it is foreign key make it as required this   means that you cannot pass car model value or you  cannot add car model data without specifying the   value for this car company ID now if I delete the  car company ID or the corresponding data alongside   with it should be deleted so that's why we using  delete behavior. Cascade you can decide to choose   it as no action and Etc but for now we want to  get rid of it as soon as the parent get deleted   all right so that is it now let's talk about  the next one that is a one to many relationship   so in here we are saying that one doctor has  many patients that is one to many and how many   patient have one doctor that is many to one now  a doctor has a a key a forign key in each of the   patients and now it is known as Dr ID before you  add a doctor you must before you add patient you   must make sure you've have or you've included of  provided value for the doctor ID so you make it as   required if I delete the parent that is a doctor  you want to um do nothing to the corresponding   record in the patient table so here we can delay  the the doctor record and now the corresponding   data in the patient was to remain we have a look  on that now that's a one to many or many to one so   from this angle it is many to one from this angle  down here it is one to many now let's have a look   with the last one that is many to many so here we  say many to many and now with this many to many relations we have the student and we have subject  so we have student in here as a parent or even   this we have a parent here they all parent they  all children so we have parent student and we have subject so before we can actually specify  configuration here we must create our um   the virtual table in this you know using the front  API we can have access to our own customizable um   table so let's get this created but how to do this  here is we're going to combine the name of the two   tables so you can see from the parent to the Chart  or from one to one we have students as you can see   from here then we have what not students yeah  student and a subject so you're going to have   student subject table so let's go in there and  create a model to handle that let's add a class this now with this class we going to say student subject so student subject we are going  to establish the the relationship in   here so you're going to write prop then we have um student then we say this is student then we have  the following key so we're going to have student   ID student ID okay so that's the forign key  as soon as we add the subject then they become   composite key because you're going to have two or  more private keys in a table so we have the next   one maybe we can just copy this and and and paste  it now we can edit this making it simple okay so   here we have subject and what do you think we're  going to have the same subject in here then inste   of the student you're going to have subject ID  so these are the composite Keys you know these   are primary keys for each of the various tables  okay so we have student subject combination of   the two tables we have them linked up in here here  we have the primary Keys State as composite keys   in here so in this we are going to handle the many  to many relationship between the student and the   subject many students can have many subject and  many subject can have many students so in here we   have a student model and you can see we have to  establish a relationship me to many relationship   in here and before we do that we have to create  um the two tables here so can see we have the   first model for students and we have the subject  for the second model So within the convention is   going to create a virtual table but using the  fluent API we can um create it ourself okay so   we have our student and our subject now the we  pick the student first and we're going to assign   the subject to it so student subject we're going  to create a model for that okay so we go to the   models folder and we create a new student subject  model and in here we are adding the student and   the student ID the subject and the subject idid  this becomes the composite key we have two primary   Keys um in one table so we have two composite Keys  once you're done with this we call it our student   subject now we have to reference the student  subject in here as a ey collection of student   subject then call it student subject we have to  make a copy of this and also paste it onto the   subject and call the same student subject when  we are done then we go to the upb contest we're   going to configure our configuration in there  so here since we have a composite key we have   to initialize them in here so this entity which  is the student um subject model it has two keys   what are this keys they are the student ID and  the composite that is a student subject subject   ID and a student ID these are the composite key  now when we are done we're going to um configure   them one after the another so first we start  with a student now student has many student um   subject that is a combination of a list of the  student and the subject it has a primary key   in it called student ID it is required and if I  delete a data record against this I do not want   to perform any action so I want to maintain the  correspondent data I do same thing to the subject   and the subject also has many of the student  subject it has a special key and that's the   forign key as subject ID it is required and if I  delete it nothing to happen Okay so this is what   we need to actually do it this is a little bit  comp little bit complex than one to many and how   many to one so that's a way to configure this  so these are the fluent API configuration and   now here you can see we can have hand over the mod  Builder we can configure it and see it to where we   want okay so now we save this in here here then we  are going to apply migration to this and check it   out we're going to see how these tables are going  to modify in here okay but before that I have the   database created I'm going to delete it maybe let  me take you through what we did from this model so   you can see from the car model you create a car  company then you know one to one so you have to   reference it in here and from the car model to  you have to make a reference since is a chart   we need to specify the parent ID as forign key we  came to doctor and our patient the doctor is a um   one to many so there's the parents in here and as  you can see you make a reference as a list you go   back to the doctor or the patient and then you're  going to provide a doctor with an ID making it as   a primary key or making as a compulsory for this  patient to be added and add the doctor ID as well   okay and now we came to this many to many that  a student and our subject okay so SQL Server I   have a database in here open this and that is  a test DB so I'm going to remove this oh that   is a EF C DB so yeah so this one I'm going to  remove this removed now let's go to let's see   if that is what we deleted yeah that is it one  so the migration to we are going to remove the   the migration now let's build the project in here  and we're going to apply new migration once it is done all right so BRD are seeded  now let's apply migration to this so we're going to have ADD  migration this will be the first one okay okay so the file is now created  have car companies the first one doctor   table student table subject and now we have  student subject car model patient now see you   have student subject table okay in that you  have two composite keys and these are the two   composite key student ID and the student subject  ID in that you have these and the constraints in   here as you can see um we have them specified  in here okay then we are going to apply or update database all right so this is done  now since we have our controller and   everything ready we can just  um run this app and check it out okay so let's see then we have a  company so company here try this out   and it's still this going to work as the  default one so company name here we have Toyota this is a company we execute this  you get this added to a database so saved   we can add I added can in here and we have  two model two companies let's add models   to this since it is one to one it means one  model to one company and now we have campan   ID of one Toyota and I'm going to say this  is VD so save this being added now if I add   it again the same thing there're going  to be an issue here yes because one to   one it has been assigned already so I have  to make here two and now this here it is abona so that is for two kantanka I'm going to  assign this see if I click on it again you're   going to have an issue here so we are done  with this it is working now when you go to   test models we can get all of these models in  here so we have Car Company ID so that's you   can see from here talking about we're getting car  modos we did not include so maybe you can quickly   go in there and I include this let's go to the  repo and now from this repo we are getting the   car modos okay so the car model we got a car  company we cannot include anything because we   did not include it and now from the car models  that is where we can include the parent so do include then we need company so do company we  can include this now when we save this and I'll   check this out let's see if this gets refreshed  now from the model we can just get execute and   you're going to have access to so we have this  click error we can just quickly go to car model   go to the car models where you have the list  in the car campany as a parent we have to so   we have this model car company and we have this  car model so let's initialize this ignore on top let's save this then let's check this out  again and see if this is going to work yeah so   we have the car company there is a vit is a  model from Toyota and the company ID is one   abona is a model from kantanka and then the  ID here it is two so we have this and it's   working as expected we can also add it when  you go to get company we can execute this and   from here we have the ID and we have the name so  we need not to include because you not have any   reference made in here okay so we have the two  models in here since this is a parent this can   exist without um the model but the model cannot  without this okay so that is it for this one now   let's go to the doctor to and there the one to  many so one doctor we can add this so we have Frederick there is a doctor programming  doctor okay so you have it saved we can   also have only this Asante here that  is it execute this we can have only   Fred execute this you see so it's  saved now we're down here let's go   and add patient so for patient you need  to provide an ID so this patient of net code and now the doctor ID here if you provide  zero see what happens going to have an error   here we need to provide ID and we can assign  one doctor to many because it is one to many   okay this next code if I only write a next code  and I click on execute is it going to be saved   let me just make the next one so I here Dr ID  of one is to get saved so now you're going to   change this to your name now your name you change  ID to two and now check it out it is working we   can have John as a patient Dr ID of three and  now check it out it is there saved we can have Jason then the doctor ID of three again now  it is saved isn't it now let's go in there   and I retrieve the doctors or the patient info  or the list so get patient include this since   we included the doctors we see we have the  doctor ID and as you can see you see that a   doctor name fed scientist appearing more than  one because here it is one to many it means   one doctor can be assigned to many patients so  first patient is net code H it has fed Santa   as Doctor second has net code it has the same  from as Doctor the third is asan as the name do   patient and it also has the same thing you can  see doctor ID one one one yeah so th so as you   can see from the next one you have your name  and Asante John and then Frederick and Jason   and fre so you see that here we have one to  many and many to one we can add data to it we   can also retrieve data from this as well okay  now let's go to the last one that is a many to   many now with the when you go to a doct test  too you can also this is post get doct T and   here we getting only only doctors okay so when  we check the code from the repository we have   get doctors and I we need not to include patient  because here there's no patient here we include   patient to the patient or we include doctors to  the patient because here the not create this is   one to many so um this is one and then this is  many so we can have we have to put the include   in here okay now let's see from the student  we can add student to this let's check it out so here we have student let's ask student  so check this out now we made this as null did   we do that student supposed to be null that is it  subject so null fine now let's check it out from here so we can remove all this from here okay  now as that we can see we need to provide subject   ID now we are in the student subject ID so first  of all we need to provide we need to fill in the   subject ID first the student subject it is a table  that we need to fill first and now when we are   done we can provide values to this if not let's  pass in the student name as frad then the subject   ID is zero click on execute and now let's see you  see it is required so let's see we have to maybe   we can remove this from here okay execute this  okay so this is saved we have subject or student   I ID Z and now this is saved now let's see let's  go to the subject try this out let's remove all this okay so you we see  that here it is coming from this so we can pass in the a subject may be  signed click on execute now this also saved   you see now let's click on get subject  and student so get subject now check it   out we see we have the subject at  science but I do not have student   subject okay because we did not provide  any value to this now let's see this one get student execute this now also we have the  student name we not having the student subject   okay so in order to do this we need to um add data  to this and how do we add data we have student ID   one we have subject ID one so based on that data  that's how we can add to the student subject list   okay let's add more student to this so let's  see student one I have Frederick I have net code and can decide to remove this CU  it is not get saved we need only the   student name click on execute saved and  I let's see let's add one John let's add   this James okay so you have our link as  I think four or five guest students so   we have about one up to four okay good now  let's see let's add four subjects to this   as well let's go we have science already  added and talk about English English as a language we can add this can have friends then we can have maybe Arabic so we have four now let's check it  out so click on get subjects execute this   we also have four isn't it good now let's go  in there and I'll add but before we do that we   need to create a model to handle this so we  go to this solution and now in here we have   our we need to create a to handle this so I'm  going to create a folder here let's stop this first let's stop this first so in here we are  going to create a model to handle this now this   what we need to do I click on the models and I'll  add a new class so here we're going to say it is   what name should should we even give it to this  okay let's say this is uh test model now the test   model we are having and the ID so this is student  ID okay then we can have the next one as subject ID subject ID now let's  save this let's come to the rppo and we going to add one here so  we say this is public asnc task so add student and subjects and I we have  this test and it is test so in here   we're going to say that a wait not  even a wait appdb contest dot then   we didn't have student subject here dot  add so what are we adding we are adding test so why are we not why are we having this  you see because we need to provide this not what   we want we need um students subject so we can  create new we can say that this is equal to new   then student subject then we can specify you know  here we have student model we are not providing   student model because we have it already we have  subject we providing subject we have already so   what you need to do here is you provide student  ID then we assign this equal to this test coming   in do student ID you see then the next one here we  have the next one that need to provide the subject   ID now we are not providing this because student  because when you check this we assign it as null   student subject we see this is null so it means  you can add data to this without specifying the   values for this if you don't do this you're going  to have an error here take note so we need not to   make this as null and to make this work okay so  once we are done with this we can now save this   then let's I wait abdb contest. save changes async  okay so you do this and let's create a controller   to handle this so you go to the solution you go  to test solution test controller and now with this   we going to have access to post and now get so the  first one here it is get we can let's make this as post and then that is student student subject so student subject okay then  here we can talk about student subject   Also let's me this in caps and  now it needs a model known as test and I will have to pass  in the test in here yeah then   we say as student subject then pass in the test so dot and student and subjects  now this needs it is saying that canot   convert from void to this yes  so we do not need to this a void type so it's tells you that here you  must have this in here so you can return okay so saved okay so there is what we want now when  it comes to the get do we have to get from here   or we can get from this okay we can get from the  table so let's get from this table and this is get so here it is get student subject get stent subject and we can just copy this okay so get stent subject and now  return. getet subject we did not include   the method in here so when to go to  repository we have to get a method   here to handle that we can say public  ASN task and this is list of student subjects you see so student subject then it must be an opening  here to period student subject must   be in upper please okay then let's say get student subject okay so in in this we can  just point this up to await updb   Contex dot student subject dot here to list asnc   can we include we subject contains the  student and also subject so do include student student and now we can also  include a subject so let me put it   down here so after student we also want to include again then subject subject then. two list async okay so that's that's what you  want to do we get this as a list then   we need to terminate this okay or we  can actually put this in a line like   this vertical coding well we can try this  today okay so we have this and when you   go to the controller let's see what we can  do from the controller instead of this we   going to do get students subject that's  fine let's run this and check check it out so you can see from here that we've added  data in here student and subject but when you   click on gu student gu subject we don't see the  correspondent student or the subject alongside   with it so we need to add the student subject  so let's add student subject when that we have   student ID of one subject ID of one remember  that's the English language let's add execute   this and now you're going to have save you're  going to SA save to the table let's wait and   see is you have save let's have then ID of  one again then sub ID of two the same student   is offering two again subject offering French  the third one is Arabic or now the fourth one   is is French or so so we have it set now if  I pass in here as five let's see do we have   five in here because we have an error because  the for key we do not have five in the subject   as you can see from here okay now let's see we  have student ID of two and this person has can   perform four subject ID of four that is an Arabic  you can just find a subject and see okay so get subject okay so we need to have this we  have this cylic error we need to work on   this this for the subject so let's quickly  work on that then we come back again it's   very important so from the subject  n here you know we have to use Jason ignore yes and now this has to from the subject get subjects try this out and  execute this yes see we have science   English French and Arabic right so here  we are adding again student ID of two is 73 so let's see student ID of three is having  St ID of one that is science it also having   Arabic that is four because it's coming from  this and and is also having french that's three and the last student that is the four  student is having only English that is two   click on Save okay so now we are done let's go  to the student so from the student we can get   all subject in here now let's go to the student  and check it out so try this out get all students   we have Cy error and we have to also do this  I think we can manage this in the service in   the uh program. CS to skip all this but for now  let's copy this let's also put it on top of the student save this and this also solved now let's  get student get execute and I can see here we have   the student and when you check the do student  and subject subject where the subject then we   have subject to so that's what we provided now  let's go to gu student and subject together so   let's execute this and I'll check it out you  can see have student info here the ID is one   the name is Frederick as you can see from  this right we have student ID of one the S   is of is French is science subject ID is one  that's Frederick when you go to the next one   student ID of John offering the subject ID of  three that is science and that is one when you   go to student ID of One Frederick offering the  subject of what English language so student ID   of one check it out student ID of One Frederick  FR is offering what science here offering English   here again that's idea of one English again and  the same person here Frederick is offering French   you see that now when you go to student ID of  four that is James offering English sent ID of   fall James offering English check it out sent  ID this subject ID you want to find student ID   this three so stent ID of three subject ID of  four okay so this offering only one when you   go to stent idea of three that is John do is  offering fr the same person here stent ID of   three you see is also offering Arabic so you  can see that many many relationship has been   established in here and now we can have access  to this we can delete the record we can update   you can modify you can do a whole lot of things  to it using the fluent a so this is more flexible   than using conventions and navigation um  property all right I hope I hope this is   clear yeah I want to end here thank you so much  for watching I believe you've learned something   new today and yeah I'm also happy to to see that  all right in the next video we're going to talk   about something that you're going to love it so  then stay tuned and don't forget to subscribe as   well and I like this video too take care of your  and I'm going to catch you up in the next video
Info
Channel: Netcode-Hub
Views: 275
Rating: undefined out of 5
Keywords: web assembly, .net 8, .NET 8, API, Develop, Blazor, WebAssembly, implementation, controller, .net blazor, nugget package, create package, install package, download, clipboard, clipboard in blazor, copy in blazor, read clipboard in blazor, validation, form validation, editform validation, clean arhitecture, presentation, domain, application, infrastructure, MediatR, CQRS, CRUD, Handler, Query, relationship, default, convention, navigation property, fluent api, efcore, onmodelcreating
Id: ldo7JIU24o8
Channel Id: undefined
Length: 102min 18sec (6138 seconds)
Published: Wed Feb 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.