![]() The ForeignKey attribute takes one parameter of type string. So let’s first create another class in the Entities project, named StudentDetails: The easiest way to configure this type of relationship is to use by the Convention approach, and that is exactly what we are going to do. This is not that common relationship because it is usually handled as “all the data in one table”, but sometimes (when we want to separate our entities) it is useful to divide data into two tables. The one-to-one relationship means that a row in one table can only relate to one row in another table in a relationship. The optional relationship is a relationship where a foreign key could be null and therefore the principal entity can be missing. This means that the principal entity must exist. The required relationship is a relationship where a foreign key cannot be null. Our entity classes will contain Navigational properties which are the properties containing a single class or a collection of classes that EF Core uses to link entity classes.Īdditionally, let’s explain the Required and Optional relationships in EF Core. The Dependent entity, from the other side, is the entity that holds the foreign key that refers to the principal entity’s primary key. It contains a primary key as a property that the dependent entity refers to via the foreign key. The Principal entity is the main entity in a relationship. When we create a relationship between two entities, one of them becomes the Principal entity and another one is the Dependent entity. But before we do that, it is quite important to understand some basic concepts when working with relational databases and models. Right now, we have only one entity (model) class, the Student class, but soon enough we are going to create the rest of the database model in our application. EF Core Relationships – Concepts and Navigational Properties To see all the basic instructions and complete navigation for this series, visit Entity Framework Core with ASP.NET Core Tutorial. You can download the source code for this article on our GitHub repository. We are going to use all three ways: by Convention, Data Annotations, and Fluent API, to create those relationships. We will show you how to create additional entities in the database model and how to create relationships between them. So as a logical continuation, this article will be dedicated to learning about database relationships configuration with Entity Framework Core (EF Core Relationships).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |