2016-03-23 88 views
0

我正在分配兩個對象之間的關係,但我在映射中收到錯誤。一對一關係實體框架外鍵映射

我有以下對象:

public abstract class EntityBase : IEntity 
{ 
    public int Id { get; set; } 
} 

public class ManagerUser : EntityBase 
{ 
    public string UserCode { get; set; } 
    public string Title { get; set; } 
    public virtual Staff StaffDetails { get; set; } 
} 

public class Staff : EntityBase 
{ 
    public string UserCode { get; set; } 
    public string DOB { get; set; } 
} 

public class ManagerUserMap : EntityMapBase<ManagerUser> 
{ 
    protected override void SetupMappings() 
    { 
     base.SetupMappings(); 
     //this.HasKey(t => t.UserCode); 
     this.ToTable("ManagerUsers"); 
     this.Property(t => t.Id).HasColumnName("ManagerUsersID") 
     .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     this.Property(t => t.Title).HasColumnName("txtTitle"); 
     this.HasRequired(x => x.StaffDetails) 
      .WithMany() 
      .HasForeignKey(x => x.UserCode); 
    } 
} 

public class StaffMap : EntityMapBase<Staff> 
{ 
    protected override void SetupMappings() 
    { 
     base.SetupMappings(); 
     //this.HasKey(t => t.UserCode); 
     this.ToTable("TblStaff"); 
     this.Property(t => t.Id).HasColumnName("StaffID") 
     .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     this.Property(t => t.UserCode).HasColumnName("User_Code"); 
     this.Property(t => t.DateOfBirth).HasColumnName("dob"); 
    } 
} 

我收到以下錯誤:

The type of property 'UserCode' on entity 'ManagerUser' does not match the type of property 'Id' on entity 'Staff' in the referential constraint 'ManagerUser_StaffDetails'

找遍四周,沒有找到解決辦法得到它比較的外鍵在ManagerUserUserCode財產Staff而不是ID財產。

+0

要指定用戶代碼爲兩個ManagerUser和工作人員,並在在Sametime主鍵,你陳述用戶代碼是爲員工外鍵... –

+0

你不能在EF6中做到這一點。 EF6只能爲關係「主體」的PK創建FK。 –

回答

0

您的密鑰在流暢配置中有點混亂,HasKey爲實體設置主鍵。在下面,我已經爲這兩個實體設置了主鍵爲Id。然後使用用戶代碼作爲FK:

public class ManagerUserMap : EntityMapBase<ManagerUser> 
    { 
     protected override void SetupMappings() 
     { 
      base.SetupMappings(); 
      this.HasKey(t => t.Id); 
      this.ToTable("ManagerUsers"); 
      this.Property(t => t.Id).HasColumnName("ManagerUsersID") 
      .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
      this.Property(t => t.Title).HasColumnName("txtTitle"); 
      this.HasRequired(x => x.StaffDetails) 
       .WithMany() 
       .HasForeignKey(x => x.UserCode); 
     } 
    } 

    public class StaffMap : EntityMapBase<Staff> 
    { 
     protected override void SetupMappings() 
     { 
      base.SetupMappings(); 
      this.HasKey(t => t.Id); 
      this.ToTable("TblStaff"); 
      this.Property(t => t.Id).HasColumnName("StaffID") 
      .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
      this.Property(t => t.UserCode).HasColumnName("User_Code"); 
      this.Property(t => t.DateOfBirth).HasColumnName("dob"); 
     } 
    } 
+0

我在帖子中編輯了代碼。 UserCode不是任何表的主鍵。 在數據庫中,Staff.UserCode鏈接到ManagerUser.UserCode。 我想我需要以某種方式告訴它將ManagerUser.UserCode鏈接到Staff.UserCode而不是ManagerUser.UserCode到Staff.ID。 –

+0

嘗試更新的代碼。確保你的數據庫是最新的。 –