繁体   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