簡體   English   中英

EF6並不總是生成FK參考

[英]EF6 not always generating FK references

我使用的是Entity Frameworks 6 ,當我從數據庫對工程圖進行反向工程時,發現沒有生成幾個FK關系。 它讓我發瘋。 如果手動添加它們,則在嘗試插入時會開始出現密鑰沖突錯誤。 將它們收回,一切正常。

架構: 數據庫架構

當我查看RemarketingCase對象時,看不到地址,債務人或便箋的任何集合,也沒有引用資產或摘要。 但是,當我查看任何一個Status對象時,我都有一個RecordHistories / CaseHistories的ICollection。 我查看的是“歷史記錄”表,但我引用了“狀態”表,但沒有引用“ RemarketingCase”表。

當我嘗試處理不存在的對象圖時,這真的很討厭! 強迫我一次按正確的順序處理一個RemarketingCase的添加及其所有關系,以使SQL不會彈出參照完整性錯誤。

帶有FK參考的樣本(由EF生成):

public partial class Status
{
    public Status()
    {
        this.CaseHistories = new List<CaseHistory>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public virtual ICollection<CaseHistory> CaseHistories { get; set; }
}
public class StatusMap : EntityTypeConfiguration<Status>
{
    public StatusMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Title)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(15);

        this.Property(t => t.Description)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("Status");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.Description).HasColumnName("Description");
    }
}

沒有由EF生成的FK參考的樣本:

public partial class RemarketingCase
{
    public int LoanId { get; set; }
    public int RequestId { get; set; }
    public string VIN { get; set; }
    public int RemarketingCaseId { get; set; }
    public string CaseId { get; set; }
    public string CaseNumber { get; set; }
    public string AssetId { get; set; }
    public System.DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> Updated { get; set; }
    public string UpdatedBy { get; set; }
}
public class RemarketingCaseMap : EntityTypeConfiguration<RemarketingCase>
{
    public RemarketingCaseMap()
    {
        // Primary Key
        this.HasKey(t => new { t.LoanId, t.RequestId, t.VIN });

        // Properties
        this.Property(t => t.LoanId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(t => t.RequestId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(t => t.VIN)
            .IsRequired()
            .HasMaxLength(25);

        this.Property(t => t.RemarketingCaseId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(t => t.CaseId)
            .HasMaxLength(50);

        this.Property(t => t.CaseNumber)
            .HasMaxLength(50);

        this.Property(t => t.AssetId)
            .HasMaxLength(50);

        this.Property(t => t.CreatedBy)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.UpdatedBy)
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("RemarketingCase");
        this.Property(t => t.LoanId).HasColumnName("LoanId");
        this.Property(t => t.RequestId).HasColumnName("RequestId");
        this.Property(t => t.VIN).HasColumnName("VIN");
        this.Property(t => t.RemarketingCaseId).HasColumnName("RemarketingCaseId");
        this.Property(t => t.CaseId).HasColumnName("CaseId");
        this.Property(t => t.CaseNumber).HasColumnName("CaseNumber");
        this.Property(t => t.AssetId).HasColumnName("AssetId");
        this.Property(t => t.Created).HasColumnName("Created");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.Updated).HasColumnName("Updated");
        this.Property(t => t.UpdatedBy).HasColumnName("UpdatedBy");
    }
}

手動添加參考的示例:

public partial class RemarketingCase
{
    public RemarketingCase()
    {
        this.Addresses = new List<Address>();
        this.Debtors = new List<Debtor>();
        this.Notes = new List<Note>();
        this.RecordHistories = new List<RecordHistory>();
        this.CaseHistories = new List<CaseHistory>();
    }

    public int LoanId { get; set; }
    public int RequestId { get; set; }
    public string VIN { get; set; }
    public int RemarketingCaseId { get; set; }
    public string CaseId { get; set; }
    public string CaseNumber { get; set; }
    public string AssetId { get; set; }
    public System.DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> Updated { get; set; }
    public string UpdatedBy { get; set; }
    // manually added...
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Debtor> Debtors { get; set; }
    public virtual ICollection<Note> Notes { get; set; }
    public virtual ICollection<RecordHistory> RecordHistories { get; set; }
    public virtual ICollection<CaseHistory> CaseHistories { get; set; }
    public virtual Asset Asset { get; set; }
    public virtual Summary Summary { get; set; }
}

這是我手動添加FK時遇到的錯誤:

測試方法AutoPay.Test.Remarketing.DatabaseChange.InsertAsset引發異常:System.Data.Entity.Infrastructure.DbUpdateException:更新條目時發生錯誤。 有關詳細信息,請參見內部異常。 ---> System.Data.Entity.Core.UpdateException:更新條目時發生錯誤。 有關詳細信息,請參見內部異常。 ---> System.InvalidOperationException:ReferentialConstraint中的從屬屬性映射到商店生成的列。 列:“ RemarketingCaseId”。

通過稍微更改數據庫設計,將可怕的ID字段添加為PK,此錯誤就消失了。 盡管這違反了SQL規范化,但實際上是這樣。 不幸的是,我還沒有找到使EF正確處理復合PK的方法。 (請參閱對我的問題的評論。)

暫無
暫無

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

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