In this chapter I will introduce code first to map the reference relationship between classes to the default rule for a one-to-many relationship between data tables. It mainly consists of the following two parts:
1.Code first maps a reference relationship between classes to the default rule for a one-to-many relationship between data tables.
2. Use the fluent API to change the foreign key's nullable property and the name of the foreign key.
3. Use the fluent API to build multiple foreign keys between two one-to-many data tables.
4. Use the fluent API to set the Cascade Delete feature.
1. Code first handles the default rule for a one-to-many relationship
My example is a simple order management system where we have two entities for order and order entry. There is a one-to-many relationship between them; an order contains multiple entries, and an entry belongs to only one order.
According to our business logic we have created the following two classes:
The first one is the order entry class:
Public classOrderItem
{
PublicintOrderitemid {Get;Set; }
PublicOrder Order {Get;Set; }
PublicList<product> Products {Get;Set; }
PublicdecimalRetailprice {Get;Set; }
PublicOrderItem ()
{
Products =NewList<product> ();
}
}
Code First06---a one-to-many relationship in Codefirst