我正在分配兩個對象之間的關係,但我在映射中收到錯誤。一對一關係實體框架外鍵映射
我有以下對象:
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'
找遍四周,沒有找到解決辦法得到它比較的外鍵在ManagerUser
與UserCode
財產Staff
而不是ID
財產。
要指定用戶代碼爲兩個ManagerUser和工作人員,並在在Sametime主鍵,你陳述用戶代碼是爲員工外鍵... –
你不能在EF6中做到這一點。 EF6只能爲關係「主體」的PK創建FK。 –