Entity Framework many to many Relation Mapping (Entity Framework Many-to-many relationship mappings)

Source: Internet
Author: User

usually when we do database design there will be two tables is a many-to-many relationship, when the database long-to-many relationship when we usually through the Intermediate association table to deal with, then we are now in How is it handled in EF?

Suppose we have the following relationship, the user ( User) contains multiple roles (role), how do we use EF to handle such a database design when the role contains multiple users?

Next look at the following code listing:

first of all, look at our User,Role entity

1  Public classUser2 3 {4 5      PublicUser () {}6 7  8 9      Public intUserId {Get;Set; }Ten  One      Public stringName {Get;Set; } A  -      Public stringPassword {Get;Set; } -  the      Public stringPasswordSalt {Get;Set; } -  -      PublicByte Status {Get;Set; } -  +      PublicDateTime CreatedDate {Get;Set; } -  +      PublicDateTime ModifiedDate {Get;Set; } A  at } -  -  Public classRole -  - { -  in       PublicRole () {} -  to       Public intRoleid {Get;Set; } +  -       Public stringRoleName {Get;Set; } the  *}

Next, we know that the user has multiple roles, then add such code in the User entity

Public virtual icollection<role> Roles {get; set;}

in the add such code in the User's constructor

This. Roles = new hashset<role> ();

Similarly, the role also contains multiple users, then the add such code to the Role entity

Public virtual icollection<user> Users {get; set;}

in the add such code to the Role constructor

This. Users = new hashset<user> ();

Next, we construct our inheritance from DataContext class of DbContext class

public class datacontext:dbcontext{public    DataContext ()        : Base ("DataContext")    {    } public    DbSet <User> Users {get; set;}    Public dbset<role> Roles {get; set;}    protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)    {            }}

Next, we build our project and we can see the database relationships we've generated.

you can see that the link table in our midst is generating User_userid,Role_roleid, but we may not be able to define this in the actual work, or we want to be more customizable, and then we'll use the Fluent API (Note: I literally translate this as a fluent API statement, you can see the literal meaning of understanding, I do not necessarily the right) way to make the data declaration, we have a relationship here is A user can contain multiple roles, a role can also contain multiple users , we write this in the Onmodelcreating overloaded method of DataContext:

Modelbuilder.entity<user> ()    . Hasmany (r = r.roles). Withmany (U = u.users);

Further we specify the intermediate link table

Modelbuilder.entity<user> ()     . Hasmany (r = r.roles)     . Withmany (U = u.users)     . Map (ur =     {         ur. Mapleftkey ("UserId");         Wrl Maprightkey ("Roleid");         Wrl ToTable ("Userroles");     });

Resources:

Http://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html

Entity Framework many to many Relation Mapping (Entity Framework Many-to-many relationship mappings)

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.