Master .NET 8 EFCore Relationships (1:1, 1:N, N:N) Mapping with Convention and Navigation Properties

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 121 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 mod 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 about 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 students 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 me 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 does not give a direct access  to this many to many we're going to use the fluent   API so when we are done with this we 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 this 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 now create the car model so with the car mod we going to have the  same ID for primary key then you're 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 since 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 the 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  12 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 t 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 model   when adding cap company so make it as snable the  same thing applies to this car model you have to   make this also assignable 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   keyy 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 model 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 our 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 called DB trans connection to true and our   trust server cerificate 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 Constructor 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 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 ABB contest that's the app 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 a 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 write 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 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  model 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 n 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 C 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 a 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 our 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 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 do 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 void let's check the repository 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 scope 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  SK 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 [Music]  have this Canan now let's add this execute then within this kantanka we can add this so we have the typ 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 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 when 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 too many so you're going   to provide list so you can use list or our  collection 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 to 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 [Music] provide doctor make itable and   we say this is Doctor and we need to provide an  ID so this is dror 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 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 you 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 duct 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 kante 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 have 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 do  one check it out saved now let's have patient   for Doctor 2 doctor 2 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 the 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 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 can 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 now 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 we 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 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 meure 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 contest 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 you 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 repair 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 ignor Jon this is a 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 that subject subject so subject. add  subject that is it let's create our 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 is 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 guest 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 add 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 subject 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 Hub 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 guest  student you can see that we having this   student but we 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 too 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 rppo in here let's see from the repo  where is the repo that is this one you know from   the student we 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  as subject ID 1 okay now student ID 1   has subject ID 2 student ID 2 has subject ID one they have 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 Che 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 I believe this   is clear thank you so much for watching um this  video good catch up again till then take care
Info
Channel: Netcode-Hub
Views: 1,179
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, file download in blazor, file upload in blazor, clean arhitecture, presentation, domain, application, infrastructure, MediatR, CQRS, CRUD, Handler, Query, relationship, default, convention, navigation property
Id: 3ziI4oTiex0
Channel Id: undefined
Length: 61min 57sec (3717 seconds)
Published: Sun Feb 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.