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