The main objective of the function is to do common task replication. Either a table or a single value is returned by the function. Input parameters are accepted by the function and then action is performed and then finally the result is returned. The following code configures the cascade delete using the WillCascadeOnDelete method.In SQL, server functions are the objects of the database in which there is a group of SQL statements for a particular task. For example, if Grade is deleted then all the students in that Grade should also be deleted automatically. To make it a NotNull column, use the HasRequired() method as shown below.Ĭonfigure Cascade Delete using Fluent APIĬascade delete means automatically deleting child rows when the related parent row is deleted. In convention 1, we have seen that it creates an optional one-to-many relationship which in turn creates a nullable foreign key column in the database. The above example will create the following tables in the database.Ĭonfigure the NotNull ForeignKey using Fluent API The following code produces the same result as above. Specifies the foreign key property in the Student entity.Īlternatively, you can start configuring the relationship with the Grade entity instead of the Student entity. Now, if the Student entity does not follow the Id property convention for foreign key, then we can specify the name of the foreign key using the HasForeignKey method.Here, many infers the ICollection type property. Specifies that the Grade entity class includes many Student entities. Now, it's time to configure the other end of the relationship - the Grade entity.This will create a NotNull foreign key column in the DB. HasRequired(s => s.CurrentGrade) specifies that the Student entity has required the CurrentGrade property. So, modelBuilder.Entity() starts with the Student entity. First, we need to start configuring with any one entity class.Let's understand the above code step by step. Protected override void OnModelCreating(DbModelBuilder modelBuilder) You can configure a one-to-many relationship for the above entities using Fluent API by overriding the OnModelCreating method in the context class, as shown below. You can configure NotNull foreign key using fluent API.Īnother convention is to include a collection navigation property in the principal entity as shown below. Notice that the reference property is nullable, so it creates a nullable foreign key column Grade_GradeId in the Students table. Where the Students table includes foreign key Grade_GradeId as shown below. This will result in a one-to-many relationship between the Students and Grades table in the database, So, there can be many students in a single grade. In the above example, the Student class includes a reference navigation property of Grade class. This can be achieved by including a reference navigation property of type Grade in the Student entity class, as shown below. It means that each Student entity points to a Grade. We want to establish a one-to-many relationship between the Student and Grade entities where many students are associated with one Grade. Let's look at an example of all the conventions which create a one-to-many relationship. You don't need to configure anything else. There are certain conventions in Entity Framework which if followed in entity classes (domain classes) will automatically result in a one-to-many relationship between two tables in the database. The one-to-many relationship can be configured in the following ways.Ĭonventions for One-to-Many Relationships Let's configure a one-to-many relationship between the following Student and Grade entities where there can be many students in one grade.Īfter implementing the one-to-many relationship in the above entities, the database tables for Student and Grade will look like below. Here, we will learn how to configure One-to-Many relationships between two entities (domain classes) in Entity Framework 6.x using the code-first approach. Next Configure One-to-Many Relationships in EF 6
0 Comments
Leave a Reply. |