Entity Framework 4.1: Multi-to-many relationship, entityframework

Source: Internet
Author: User

Entity Framework 4.1: Multi-to-many relationship, entityframework

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.

This article discusses many-to-many relationships.

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 ;}
Public virtual List <Employee> InvolvedEmployees {get; set ;}
}

Public class Employee
{
Public int EmployeeID {get; set ;}
Public string EmployeeName {get; set ;}

Public virtual List <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

The following code can be used:

ModelBuilder. Entity <Employee> ()
. Hasders (e => e. Orders)
. Withye( 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 ManyToMany ()
{
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.

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

Related Article

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.