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.
- One of Entity Framework 4.1: Basic
- Entity Framework 4.1 II: overwrite the default conventions
- Entity Framework 4.1 3: greedy loading and delayed Loading
- Entity Framework 4.1 4: complex types
- Entity Framework 4.1-5: Multi-to-many relationships
- Entity Framework 4.1: Optimistic Concurrency
- Entity Framework 4.1 7: Inheritance
- 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.