[英]EF6 not always generating FK references
I am using Entity Frameworks 6 and when I reverse engineer my schema from the database, I am finding that several FK relationships are not being generated. 我使用的是Entity Frameworks 6 ,当我从数据库对工程图进行反向工程时,发现没有生成几个FK关系。 It's driving me nuts. 它让我发疯。 If I add them in by hand I start getting key violation errors when trying to insert. 如果手动添加它们,则在尝试插入时会开始出现密钥冲突错误。 Take them back out and everything works fine. 将它们收回,一切正常。
Schema: 架构:
When I look at my RemarketingCase object I do not see any collections for Addresses, Debtors or Notes and no reference to Asset or Summary. 当我查看RemarketingCase对象时,看不到地址,债务人或便笺的任何集合,也没有引用资产或摘要。 Yet, when I look at either Status objects I have an ICollection of RecordHistories/CaseHistories. 但是,当我查看任何一个Status对象时,我都有一个RecordHistories / CaseHistories的ICollection。 I look at either History tables and I have reference to the Status table but no reference to the RemarketingCase table. 我查看的是“历史记录”表,但我引用了“状态”表,但没有引用“ RemarketingCase”表。
This is really irksome as I am trying to deal with object graphs that just don't exist! 当我尝试处理不存在的对象图时,这真的很讨厌! It is forcing me to handle the add of a RemarketingCase and all it's relationships by doing so one at a time, in the correct order so SQL does not pop a referential integrity error. 强迫我一次按正确的顺序处理一个RemarketingCase的添加及其所有关系,以使SQL不会弹出参照完整性错误。
Sample with FK reference (generated by EF): 带有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");
}
}
Sample without FK reference(s) as generated by EF: 没有由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");
}
}
Sample of hand adding references: 手动添加参考的示例:
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; }
}
And this is the error I get when I hand add the FKs: 这是我手动添加FK时遇到的错误:
Test method AutoPay.Test.Remarketing.DatabaseChange.InsertAsset threw exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. 测试方法AutoPay.Test.Remarketing.DatabaseChange.InsertAsset引发异常:System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. ---> System.Data.Entity.Core.UpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 ---> System.InvalidOperationException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. ---> System.InvalidOperationException:ReferentialConstraint中的从属属性映射到商店生成的列。 Column: 'RemarketingCaseId'. 列:“ RemarketingCaseId”。
By changing the DB design a bit, adding the dreaded ID field as the PK, this error went away. 通过稍微更改数据库设计,将可怕的ID字段添加为PK,此错误就消失了。 While this is a violation of SQL normalization it is what it is. 尽管这违反了SQL规范化,但实际上是这样。 Unfortunately, I've not found a way to make EF deal with a compound PK correctly. 不幸的是,我还没有找到使EF正确处理复合PK的方法。 (See the comments to my question.) (请参阅对我的问题的评论。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.