Entity Framework Configuring relationships with the Fluent API

Source: Internet
Author: User

RELATED links: Configuring relationships with the Fluent API

Configuring a required-to-optional relationship (One-to-zero-or-one 1 ... 0,1)

The following example, which represents 1 ... 0,1 's relationship. The OfficeAssignment table has an attribute Instructorid, which is a primary key and also a foreign key.

The Haskey method is used to configure the primary key.

modelbuilder.entity<officeassignment> ()     = t.instructorid);    //  modelbuilder.entity<officeassignment>()     = t.instructor)     and T. OfficeAssignment);

Configuring a Relationship Where Both Ends is Required (one-to-one)
modelbuilder.entity<officeassignment> ()     = T.instructorid);    Modelbuilder.entity<Instructor>()     = t.officeassignment)     and T. Instructor);

Configuring a Many-to-many Relationship

Modelbuilder.entity<course>()     = t.instructors)     = t.courses)

Modelbuilder.entity<course>()     = t.instructors)     = t.courses)     = =     {         m.totable ("courseinstructor");         M.mapleftkey ("courseid");         M.maprightkey ("instructorid");     });

Configuring a relationship with one Navigation property

modelbuilder.entity<officeassignment> ()     = T.instructorid);    Modelbuilder.entity<Instructor>()     = t.officeassignment)     . Withrequiredprincipal ();

Enabling Cascade Delete
Modelbuilder.entity<course>()     = t.department)     = t.courses)      = d.departmentid)     . Willcascadeondelete (false);

Configuring a Composite Foreign Key
modelbuilder.entity<department>New  {d.departmentid, d.name});    //  modelbuilder.entity<course>()      = c.department)      and D. Courses)     new {d.departmentid, d.departmentname});

Renaming a Foreign Key that's not Defined in the Model
Modelbuilder.entity<course>()     = c.department)     = t.courses)      = = M.mapkey ("changeddepartmentid"));

Configuring a Foreign Key Name that Does not follow the Code first convention
Modelbuilder.entity<course>()          = c.department)          = d.courses)           = C.somedepartmentid);

Model used in Samples
usingSystem.Data.Entity;usingSystem.Data.Entity.ModelConfiguration.Conventions;//add a reference to System.ComponentModel.DataAnnotations DLLusingSystem.ComponentModel.DataAnnotations;usingSystem.Collections.Generic;usingSystem;  Public classSchoolentities:dbcontext { PublicDbset<course> Courses {Get;Set; }  PublicDbset<department> Departments {Get;Set; }  PublicDbset<instructor> Instructors {Get;Set; }  PublicDbset<officeassignment> officeassignments {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Configure Code First to ignore Pluralizingtablename convention//If You keep this convention then the generated tables would have pluralized names.Modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } }   Public classDepartment { PublicDepartment () { This. Courses =NewHashset<course>(); }     //Primary Key     Public intDepartmentID {Get;Set; }  Public stringName {Get;Set; }  Public decimalBudget {Get;Set; }  PublicSystem.DateTime StartDate {Get;Set; }  Public int? Administrator {Get;Set; } //Navigation Property     Public VirtualIcollection<course> Courses {Get;Private Set; } }   Public classCourse { PublicCourse () { This. Instructors =NewHashset<instructor>(); }     //Primary Key     Public intCourseID {Get;Set; }  Public stringTitle {Get;Set; }  Public intCredits {Get;Set; } //Foreign Key     Public intDepartmentID {Get;Set; } //Navigation Properties     Public VirtualDepartment Department {Get;Set; }  Public VirtualIcollection<instructor> Instructors {Get;Private Set; } }   Public Partial classOnlinecourse:course { Public stringURL {Get;Set; } }   Public Partial classOnsitecourse:course { PublicOnsiteCourse () {Details=NewDetails (); }       PublicDetails (Details) {Get;Set; } }   Public classDetails { PublicSystem.DateTime Time {Get;Set; }  Public stringLocation {Get;Set; }  Public stringDays {Get;Set; } }       Public classInstructor { PublicInstructor () { This. Courses =NewList<course>(); }      //Primary Key     Public intInstructorid {Get;Set; }  Public stringLastName {Get;Set; }  Public stringFirstName {Get;Set; }  PublicSystem.DateTime HireDate {Get;Set; } //Navigation Properties     Public VirtualIcollection<course> Courses {Get;Private Set; } }   Public classOfficeAssignment {//specifying Instructorid as a primary[Key ()] PublicInt32 Instructorid {Get;Set; }  Public stringLocation {Get;Set; } //When the Entity Framework sees Timestamp attribute//it configures Concurrencycheck and databasegeneratedpattern=computed.1537884940 PublicByte[] Timestamp {Get;Set; } //Navigation Property     Public VirtualInstructor Instructor {Get;Set; } }

Entity Framework Configuring relationships with the Fluent API

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.