簡體   English   中英

為什么 Entity 在已經定義的情況下創建關鍵字段?

[英]Why is Entity creating a key field when it is already defined?

我有幾個相關的域模型觸發異常SqlException: Invalid column name 'ChecklistTemplate_Id'

我的域 model 如下所示:

public class Assignment
{
    public long Id { get; set; }
    public long ChecklistId { get; set; }
    public DateTime InspectionDate { get; set; }
    public long JobId { get; set; }
    public Guid? EmployeeId { get; set; }
    // TODO: Make the completion a nullable date time in the database and here
    public DateTime CompletionDate { get; set; }

    public virtual Job Job { get; set; }
    public virtual Checklist Checklist { get; set; }
    public virtual IList<Image> Images { get; set; }
    public virtual IList<Attachment> Attachments { get; set; }
    public virtual IList<Equipment> Equipments { get; set; }
}

我的 EntityTypeConfiguration class 看起來像:

internal class AssignmentConfiguration : EntityTypeConfiguration<Assignment>
{
    public AssignmentConfiguration()
    {
        ToTable("Assignment");

        HasKey(k => k.Id);

        Property(a => a.ChecklistId)
            .IsRequired();
        Property(a => a.CompletionDate)
            .IsOptional();
        Property(a => a.EmployeeId)
            .IsOptional();
        Property(a => a.Id)
            .IsRequired();
        Property(a => a.InspectionDate)
            .IsRequired();
        Property(a => a.JobId)
            .IsRequired();

        HasRequired(a => a.Job)
            .WithMany(a => a.Assignments)
            .HasForeignKey(a => a.JobId);

        HasRequired(a => a.Checklist)
            .WithOptional(a => a.Assignment);

        HasMany(a => a.Images)
            .WithRequired(a => a.Assignment)
            .HasForeignKey(a => a.InspectionId);
    }
}

Checklist 域 model 有一個帶有連接的 ChecklistTemplate 導航屬性:

HasMany(a => a.CheckLists)
            .WithRequired(a => a.ChecklistTemplate)
            .HasForeignKey(a => a.ChecklistTemplateId);

如 Assignment 實體配置中所示,Assignment 和 Checklist 之間存在一對一的關系。

是的,我們將配置包含在 DBContext 中。

此外,我查看了Entity Framework 6 創建 Id 列,即使其他主鍵已定義並且似乎不適用。

我對此沒有滿意的答案,但我在使用 ef6 時遇到了很多麻煩。 這是因為存在未定義或錯誤定義的導航。 所以 ef6 在代理類上即時創建它,你會哭幾個小時。 我希望你能盡快找出問題所在。

你說的導航是一對多的。 當心。

暫無
暫無

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

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