Code First06---a one-to-many relationship in Codefirst

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.