Entity Framework - Relationships: One-to-One, Many-to-Many, One-to-Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
database relationship in Entity Framework 6. so let's see how we can Define the database relationship with the alpha quantity framework 6 code first approach first off let's see what are the various types of relationships we have in database so we are having one to zero or one two one relationship which we can create between two tables there is a one too many and the third one is here menu to many so these are the three types of database relationship we generally defined between two database table now let's understand each and every relationship with the help of the example so let's start with one two zero or one to one relationship let's say we are having two entities we are having an employee we are having the address and will be these two entities we are willing to Define one two zero or one to one mapping so in one to one mapping an employee can have only one address or you can say if you are willing to add the address so before that you need to add the employee and if this cannot be added before adding the employee so such relationship would be the one-to-one relationship because I cannot add the entry into that dress until it's not added the employee so here the relationship we can Define by using the data notation approach and by using the followed API approach so you have two options either use the data notation convention either use the following APA convention so if you are going to use a data annotation we need to take the help of the reference navigation property so we can see here we have added a reference navigation property of address into the employee class same way we have added the reference navigation property into the address class for the employee even we made the address ID as foreignty so here at this ID would be the primary key as well as foreign key so this is the way how we can use the data notation way to define one to zero like one to many mapping between the employee and that this entity of the same thing you are willing to Define with the verb of the fluent AP approach so there is a way also so we can use the other we can use here the fluent AP approach so there you need to use the model builder then Define The Entity like the entity here first of all the employee I'm passing then there is a method has optional so as optional is representing here like zero or one so the employee in the employee like there should be a reference navigation property so this reference navigation property we are defining here using this Lambda expression so e for employee then e dot address so this reference navigation property in the employee class we are mentioning then there is a method with the required because here there is a one-to-one mapping so now let's come to that this table so a d is representing the address entity then in that this entity there is a again reference property with the employee so this reference property for the employee we are mentioning here so this is the way how we can Define one-to-one mapping using the data annotation and the following APA okay let's see in action so what I'm having I'm just opening the video Studio let's create a new project for our mappings so I'm just creating a new project here and Define a blank solution again so my solution name is let's say database mappings and Define the location so location would be here let's say it's code then Entity Framework then click on the OK button now here I did empty project so what I'm doing here I'm just adding here the empty project here just to right click and here at the new project and as a new project I'm adding here class Library using dot NET Framework then you specify the location here let's say Del so Delhi is the project which I'm willing to create here so here you can see there is a Class one which is created by default here and here we are going to Define other one to one mapping so for one to one mapping uh we need to define the entities here but before defining the entities let's install here identity framework you got package so just go to the minus and you get packages here then browse I'm just browsing here Entity framework you get packet install it in our dull layer so we have successfully installed it so now after installing the Entity framework we can use the data annotation or we can use the followed API for defining the mapping easily so as I have shown you in the ppts let me copy the same code here so that we can save the time so I'm just going to copy the code and paste here so if you will see there are two classes here employee and address so as a practice you should Define the separate class for each entity so first I have defined what employee let me add a class for address as well so I'm adding correct class for address also and let me copy that just cut the cut dress code from here and no paste here let's add the missing name spaces so let's add a system dot componentmodel dot determination dot schema name space I added here and here we are not using any data notation so we we don't need to add any name space so if you will see here this is a reference navigation property in the applied class and here we are representing the address entity so in the same at this entity there is employee one so this is the way how we Define the one to one mapping here using the data annotation and some of the mapping its we are using here as per The Entity framework default convention I now have to create the database for these two entities so first of all let's create a database context class so here I'm adding a class named as database context now let's make it public now just inherit it from DP context Now define the Constructor at the name space as well system.data dot entity and in the base class Constructor we can call by mentioning the base key one now let's define the database connections thing name it would be here default connection so this default connection we should Define as a connection string so as per my system just going to paste the database connections thing so here just Define your database name which you are willing to create let's say we are going to create the database named as database mappings perfect so now we are just willing to create this database mapx when we will do the migration here and make sure we update the address and employee here using the DB set public date B set and a DB said it would be here employee employee then get set the net here address also it's here at this Define here and now we are using the migration we can create the database with these tables and whatever the mapping we have defined we can see there in action even let's say same way that the mapping we can Define here using the fluent APS versus let's use here override I'm using here the override option then on model 30. and as we have in case of this PDF this as we have in this PPT so let me copy and paste the other things so this is the mapping we are just using here we can see so it's using it as optional so generally the people prefer to use the full entry PV when they are going to understand one two one one too many and many too many mapping details because it is it is it is easy to understand you can easily understand here what is going to be do so let's say has optional for zero unlike one it is with required for one so if this is the way for defining one two zero or you can say one to one mapping now let's do the migration using the package manager console I'm opening here package manager console and here in the command and your build as migrations so we are doing it first time so this way I'm running here this command enabled as migrations so you can see here it's enabled the migration for us with configuration file run the next command that does migration then migration name so migration name is your initial I'm willing to create certainly create a initial migration file with all the changes you can see there is a up method which is containing the method for getting the address table it is containing the method for getting the employee table however there is a relationship as well you can see it's creating that this ID is a primary key it's getting that this ID as a foreign key also same way you can see the employee ID is a primary gets creative now let's update the database update this database space this Workforce so let's see the one to one mapping in action so you can see here it's generated the SQL script here for getting the address for getting the employee table and double underscore migration let me check in the database what database is created for us it's created here database mappings and we are having here two table we are having employee table contouring the employee is a primary key name is a and worker Max in a deciphable check this address ID as a primary key as well as foreign key here so and you can see it will be looking into this foreign relationship so this formic relationship is going to be created based upon this employee ID so this employee is going to be map as a foreign key with this address ID it means this address ID will contain this employee ID so first of all you need to add a employee available at the new employee obviously you will get the employee ID that employee ID would be added here as a address ID or you can see as a foreign key also so this is the way there is a one-to-one mapping we have defined so first we need to add employee ID then you will get primary key that primary key you will add here as a primary key and there is a relationship here between so that it's going to map as a foreign key also if you want to verify it there is option here let's say address then you can go to the design and the design there is option at this ID then go to the mappings just go to the relationships here in the relationship you will find out what they have defined so there is option here for editing the things just go to the mapping in the mapping you can see the employee ID is going to map as a foreign key in that desk table so whatever the primary key we will have in the employee table the same key is going to become as a foreign key that means address ID and the employee ID would be same let's say if we have added employee one and imply this one so this ID would also be one if employee these two so this ID will also be 2. so that it would be the one to one mapping thank you for watching this video If you like this video please do share with your friends and the colleague and also do subscribe to scholar head YouTube channel and click the Bell icon so that the information about such video you will get automatically
Info
Channel: ScholarHat
Views: 17,177
Rating: undefined out of 5
Keywords: entity framework relationships, many to many relationship, one to many, many to many, one to many relationship, many-to-many
Id: TxDcnuvirCI
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Tue Jan 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.