簡體   English   中英

一對一關系Entity Framework外鍵映射

[英]One to one relationship Entity Framework foreign key mapping

我正在分配兩個對象之間的關系,但在映射上遇到錯誤。

我有以下對象:

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");
    }
}

我收到以下錯誤:

實體'ManagerUser'上屬性'UserCode'的類型與引用約束'ManagerUser_StaffDetails'中實體'Staff'上屬性'Id'的類型不匹配

我四處搜尋,但未能找到解決方案,無法將ManagerUser的外鍵與StaffUserCode屬性(而不是ID屬性)進行比較。

您的鍵在流暢的配置中有些混亂,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");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM