เหตุใดเอนทิตีจึงสร้างคีย์ฟิลด์เมื่อมีการกำหนดไว้แล้ว

Aug 20 2020

ผมมีรูปแบบโดเมนที่เกี่ยวข้องหลายอย่างที่กำลังเรียกข้อยกเว้นSqlException: ชื่อคอลัมน์ไม่ถูกต้อง 'ChecklistTemplate_Id'

รูปแบบโดเมนของฉันมีลักษณะดังนี้:

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 ของฉันมีลักษณะดังนี้:

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

แบบจำลองโดเมนรายการตรวจสอบมีคุณสมบัติการนำทาง ChecklistTemplate ที่มีการเข้าร่วม:

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

มีแบบหนึ่งต่อหนึ่งระหว่างการมอบหมายงานและรายการตรวจสอบดังที่เห็นในการกำหนดค่าเอนทิตีการมอบหมาย

และใช่เรากำลังรวมการกำหนดค่าใน DBContext

นอกจากนี้ฉันดูที่Entity Framework 6 สร้างคอลัมน์ Id แม้ว่าคีย์หลักอื่นจะถูกกำหนดไว้และดูเหมือนจะไม่เกี่ยวข้อง

คำตอบ

Noname Aug 20 2020 at 03:23

ฉันไม่มีคำตอบที่น่าพอใจ แต่ฉันมีปัญหากับ ef6 มาก เนื่องจากมีการนำทางที่ไม่ได้กำหนดไว้หรือกำหนดไว้ไม่ถูกต้อง ดังนั้น ef6 จึงสร้างมันขึ้นมาทันทีในชั้นเรียนพร็อกซีและคุณร้องไห้เป็นชั่วโมง ฉันหวังว่าคุณจะพบปัญหาในไม่ช้า

และการนำทางที่คุณระบุเป็นแบบหนึ่งต่อหลายคน ระวัง.