"EF Code First" multi-to-many relationship configuration

Source: Internet
Author: User

Here's an example of using the user table and the project table

There is such a requirement:

The relationship between the user and the project is that a user can send multiple projects, can participate in multiple projects, and the project can have multiple participating members and one publisher

Project structure:

The entity class structure is designed as follows

<summary>///user//    </summary> public class User {public              int ID {get;set;}       public string Nickname {get; set;}                     Public DateTime registertime {get; set;}       <summary>////       participation in the list///       </summary> public       icollection<project> Projects {get; Set }    }

  

<summary>///project///    </summary> public   class project    {public             int ID {get; set ; Public       string Title {get; set;}       public string Description {get; set;}       <summary>///Project Publisher///       </summary> public       virtual User founder {get; set;}       <summary>///participating Members///       </summary> public       virtual icollection<user> Member {get; Set }    }

  

Then, create a new mapping class UserMap

public class usermap:entitytypeconfiguration<user>    {public        UserMap () {//define a many-to-many relationship between user and project this            . hasmany<project> (U = u.projects). Withmany (P=>p.member);        }    }

In the EF context

Public  class Db:dbcontext    {public       DB (): Base ("DefaultConnection") {} public       dbset<user> User { Get Set Public       dbset<project> Project {get; set;}       protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)       {           modelbuilder.conventions.remove< Pluralizingtablenameconvention> ();//Remove the contract//base of the plural table name          . Onmodelcreating (ModelBuilder);           MODELBUILDER.CONFIGURATIONS.ADD (New UserMap ());       }    }

  

Class program    {        static void Main (string[] args)        {            db db = new DB ();                        Db. User.tolist ();            Console.readkey ();        }    }

After running, the table structure

"EF Code First" multi-to-many relationship configuration

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.