Загрузка...

One-to-One Relationship | One-to-Many Relationships | Many-to-Many Relationships in EF Fluent API

One-to-One Relationships in Entity Framework | Fluent API One-to-Many Relationships in Entity Framework Fluent API | Many-to-Many Relationships in Entity Framework Fluent API

Configure One-to-One Relationships in Entity Framework Fluent API
In this article, I am going to discuss how to configure One-to-Zero or One-to-One relationships between two entities using Entity Framework Fluent API with Examples. Let us understand this with an example. We are going to implement One-to-Zero or One-to-One relationship between the following Student and StudentAddress entities.
How to Configure One-to-Zero-or-One Relationship using Fluent API?
We can configure the One-to-Zero-or-One relationship between two entities using both Data Annotation Attribute and Fluent API. We have already discussed configuring One to One relationship between the two entities using the ForeignKey Data Annotation Attribute. Now, let us proceed and try to understand how to Configure One-to-Zero or One-to-One relationships between two entities using Entity Framework Fluent API. To implement this, we need to use the following HasOptional() and WithRequired() Fluent API methods.

HasOptional(): It configures an optional relationship from the entity type. Instances of the entity type will be able to be saved to the database without this relationship being specified. The foreign key in the database will be nullable.
WithRequired(): It configures the relationship to be optional:required with a navigation property on the other side of the relationship.

Configure One-to-Many Relationships using Entity Framework Fluent API
In this article, I am going to discuss how to configure One-to-Many Relationships between two Entities using Entity Framework Fluent API with Examples. Please read our previous article where we discussed how to configure One-to-Zero or One-to-One relationships between two entities. Let us understand this with an example. We are going to implement a One-to-Many relationship between the following Student and Standard Entities.

Convention 1 for Implementing One-to-Many Relationship
Our Requirement is to establish a One-to-Many Relationship between the Student and Standard entities. That means one can student can be associated with one standard and one standard can be assigned to many students. We can achieve this very easily by including a Reference Navigation Property of type Standard in the Student entity class.

Configure Many-to-Many Relationships in Entity Framework Fluent API
In this article, I am going to discuss how to configure Many-to-Many relationships between two entities using Entity Framework Fluent API with Examples. Please read our previous article where we discussed how to configure One-to-Many relationships between two entities.

How to Configure Many-to-Many Relationships in Entity Framework?
In many-to-many relationships, a row in one table, let’s say TableA can have many matching rows in another table, let’s TableB, and vice versa is also true i.e. a row in TableB can have many matching rows in TableA. In Relational Databases, we can create such kinds of many-to-many relationships by defining a third table, whose primary key consists of the foreign keys from both tableA and tableB. So, here, the Primary Key is a composite Primary key.
Let us understand this with an example. We are going to implement a One-to-Many relationship between the Student and Course Entities. That means one Student can enroll for many Courses and also, one Course can be taken by many students.

In Entity Framework Code First Approach, we can configure the many-to-many relationship between two entities in the following ways.

Chapter- Entity Framework in C#

One-to-One Relationships in Entity Framework Fluent API
One-to-Many Relationships in Entity Framework Fluent API
Many-to-Many Relationships in Entity Framework Fluent API

Text Document Links used in the Video:
Author : Pranaya Rout
Website : https://dotnettutorials.net/
One-to-One Relationships in Entity Framework Fluent API : https://dotnettutorials.net/lesson/one-to-one-relationships-in-entity-framework-fluent-api/
One-to-Many Relationships in Entity Framework Fluent API : https://dotnettutorials.net/lesson/one-to-many-relationships-in-entity-framework-fluent-api/
Many-to-Many Relationships in Entity Framework Fluent API : https://dotnettutorials.net/lesson/many-to-many-relationships-in-entity-framework-fluent-api/

#dotnettutorials #csharp #pranaya #dotnettutorialspranaya
#EntityFramework #EntityFrameworkInteroduction
#EntityFrameworkByDotNetTutorials #EntityFrameworkInteroductionByDotNetTutorials
#EntityFrameworkByPranaya #EntityFrameworkInteroductionByPranaya
#FluentAPIConfigurationsinEntityFramework
#FluentAPI ##FluentAPIByPranaya ##FluentAPIByDotnettutorial
##FluentAPIEntityFramework
#OnetoOneRelationshipsinEntityFrameworkFluentAPI
#OnetoManyRelationshipsinEntityFrameworkFluentAPI
#ManytoManyRelationshipsinEntityFrameworkFluentAPI

Видео One-to-One Relationship | One-to-Many Relationships | Many-to-Many Relationships in EF Fluent API канала Dot Net Tutorials
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять