Entity Framework 4.1-5: Multi-to-many relationships

Source: Internet
Author: User

Original article name: Entity Framework 4.1: failed to define relationships (5)

Address: http://vincentlauzon.wordpress.com/2011/04/15/entity-framework-4-1-many-to-many-relationships-5/

We can see the English tutorial recommended for Entity Framework 4.1. To help you look more convenient, we can translate it briefly. This is a series of 8 articles, and this is 5th articles.
    1. One of Entity Framework 4.1: Basic
    2. Entity Framework 4.1 II: overwrite the default conventions
    3. Entity Framework 4.1 3: greedy loading and delayed Loading
    4. Entity Framework 4.1 4: complex types
    5. Entity Framework 4.1-5: Multi-to-many relationships
    6. Entity Framework 4.1: Optimistic Concurrency
    7. Entity Framework 4.1 7: Inheritance
    8. Entity Framework 4.1: bypassing EF query ing

This articleArticleThis topic describes the many-to-many relationship.

Let's start with the simplest example. Let's use ef4.1 to infer the table ing. I modeled many-to-many relationships between orders and employees.

Public Class Order
{
Public Int Orderid { Get ; Set ;}
[Required]
[Stringlength ( 32 , Minimumlength = 2 )]
Public String Ordertitle { Get ; Set ;}
[Required]
[Stringlength ( 64 , Minimumlength = 5 )]
Public String Customername { Get ; Set ;}
Public Datetime transactiondate { Get ; Set ;}
Public Byte [] Timestamp { Get ; Set ;}

Public virtual List orderdetail > orderdetails { Get ; set ;}< br> Public virtual List employee > involvedemployees { Get ; set ;}
}

Public class employee
{< br> Public int employeeid { Get ; set ;}
Public string employeename { Get ; set ;}

PublicVirtualList<Order>Orders {Get;Set;}
}

I simply add an employee list to the order table and an order list to the employee table. Look, this is the table mapped.

Now we need to control two things:

    • Join table name
    • Two column names in the joined table

Use the followingCodeYou can:

Modelbuilder. Entity < Employee > ()
. Haswon (E => E. Orders)
. Withtasks (E => E. involvedemployees)
. Map (M =>
{
M. totable ( " Employeeorder " );
M. mapleftkey ( " Employeeid " );
M. maprightkey ( " Orderid " );
});

Basically, we say that an employee manages multiple orders, and each order involves multiple employees. Therefore, we have a many-to-many relationship. The joined table is named employeeorder, the left-click (from the employee's perspective, is the employee key) is named Employee-ID, and the right-click is named order-id.

In this way, you can control tables that are not directly mapped to classes.

This model is very simple and natural.

Private Static Void Manytoyun ()
{
Using (VAR Context = New Mydomaincontext ())
{
VaR order = New Order
{
Ordertitle = " Pens " ,
Customername = " Mcdo's " ,
Transactiondate = Datetime. Now,
Involvedemployees = New List < Employee > ()
};
VaR employee1 = New Employee {employeename = " Joe " , Orders = New List < Order > ()};
VaR employee2 = New Employee {employeename = " Black " , Orders = New List < Order > ()};

Context. Orders. Add (order );

Order. involvedemployees. Add (employee1 );
Order. involvedemployees. Add (employee2 );

Context. savechanges ();
}

In this example, I didn't even add employees to the employee collection in the data context, because they were referenced to the order collection, and EF helped us complete the process.

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.