2013-02-18 34 views
0

http://msdn.microsoft.com/en-US/data/jj591620#RequiredToRequired上的示例代碼是否正確?該代碼要求Instructor類中的OfficeAssignment prop。它不會因爲明顯的原因而解決。現在在ef上建立一對一關係的正確方法是什麼?配置兩端都需要的關係(一對一)

// Configure the primary key for the OfficeAssignment 
modelBuilder.Entity<OfficeAssignment>() 
.HasKey(t => t.InstructorID); 

modelBuilder.Entity<Instructor>() 
.HasRequired(t => t.OfficeAssignment) 
.WithRequiredPrincipal(t => t.Instructor); 


public class OfficeAssignment 
{ 
    // Specifying InstructorID as a primary 
    [Key()] 
    public Int32 InstructorID { get; set; } 

    public string Location { get; set; } 

    // When the Entity Framework sees Timestamp attribute 
    // it configures ConcurrencyCheck and DatabaseGeneratedPattern=Computed. 
    [Timestamp] 
    public Byte[] Timestamp { get; set; } 

    // Navigation property 
    public virtual Instructor Instructor { get; set; } 
} 

public class Instructor 
{ 
    public Instructor() 
    { 
     this.Courses = new List<Course>(); 
    } 

    // Primary key 
    public int InstructorID { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public System.DateTime HireDate { get; set; } 

    // Navigation properties 
    public virtual ICollection<Course> Courses { get; private set; } 
} 
+0

流利的映射是確定的,但很明顯,他們忘了告訴大家,1:1名教師也需要一個OfficeAssignment財產。 – 2013-02-18 10:36:18

+0

謝謝@GertArnold,至少我現在知道我不是唯一一個。 – Ronnel 2013-02-18 23:03:51

回答

1

本地/辦公室導航屬性存在錯誤。一些有意改名澄清 也許這....

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace one2one 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var context = new Demo(); 
     var instructor = new Instructor(); 
     instructor.FirstName = "Big"; 
     instructor.LastName = "Willi"; 
     context.Set<Instructor>().Add(instructor); 

     var office = new OfficeAssignment(); 
     office.Location = "is this where the demo broke down ? See POCO "; 
     office.InstructorUsingThisOffice = instructor; 

     context.Set<OfficeAssignment>().Add(office); 

     context.SaveChanges(); 
    } 
} 
public class OfficeAssignment 
{ 
    // Specifying InstructorID as a primary 
    public Int32 InstructorID { get; set; } 
    public string Location { get; set; } 

    // Navigation property 
    public virtual Instructor InstructorUsingThisOffice { get; set; } 
} 

public class Instructor 
{ 

    // Primary key 
    public int InstructorID { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    //navigation 
    //missing 
    public virtual OfficeAssignment TheofficeToUse { get; set; } 

} 
public class Demo : DbContext 
{ 


    DbSet<OfficeAssignment> officeAssignments { get; set; } 
    DbSet<Instructor> Instructors { get; set; } 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     // Configure the primary key for the OfficeAssignment 
     modelBuilder.Entity<OfficeAssignment>() 
     .HasKey(t => t.InstructorID); 

     modelBuilder.Entity<Instructor>() 
        .HasRequired(t => t.TheofficeToUse) 
        .WithRequiredPrincipal(d => d.InstructorUsingThisOffice); //current entity is principal, the navigation back. 
     // and we share the same key... MUST with EF 1:1 foreign key 


    } 
} 
} 
相關問題