对于给定的关系,我有以下POCO:
ProductTransferHistory [1-*] <-- ProductTransferHistoryEntry [1-*] <-- WipEntry
代码如下:
public class ProductTransferHistory
{
[Key]
public Int32 ProductTransferHistoryId { get; set; }
public DateTime TransactionDate { get; set; }
[InverseProperty(nameof(ProductTransferHistoryEntry.ProductTransferHistory))]
public ICollection<ProductTransferHistoryEntry> ProductTransferHistoryEntries { get; set; }
public IList<ProductTransferHistoryEntry> EntriesWithName => ProductTransferHistoryEntries.Where(x => x.Name != null).ToList();
}
public class ProductTransferHistoryEntry
{
[Key]
public Int32 ProductTransferHistoryEntryId { get; set; }
[ForeignKey(nameof(ProductTransferHistoryId))]
public ProductTransferHistory ProductTransferHistory { get; set; }
public Int32 ProductTransferHistoryId { get; set; }
public String Name { get; set; }
[InverseProperty(nameof(WipEntry.ProductTransferHistoryEntry))]
public virtual ICollection<WipEntry> WipEntries { get; set; }
public IList<WipEntry> NonZeroWipEntries => WipEntries.Where(x => x.Quantity != 0).ToList();
}
public class WipEntry
{
[Key]
public Int32 WipEntryId { get; set; }
[ForeignKey(nameof(ProductTransferHistoryEntryId))]
public ProductTransferHistoryEntry ProductTransferHistoryEntry { get; set; }
public Int32 ProductTransferHistoryEntryId { get; set; } // associated entry of ProductTransferHistoryId
public Decimal Quantity { get; set; }
}
出于某种原因,当我为ProductTransferHistoryEntries表添加迁移时,它会生成两个外键列:
ProductTransferHistoryId
,以及一个_ProductTransferHistory_Id_列。而且
威文垂
表1它正在创建两个FK列
ProductTransferHistoryEntryId
,以及_ProductTransferHistoryEntry _Id _。在这两种情况下,第二个FK都可以为空,这也没有意义。
问题似乎在于集合导航属性。我原以为“InverseProperty”注释可以解决这个程序,但它仍在生成额外的FK。如果删除nav集合属性,则不会生成额外的列,但这不是解决方案,因为我需要这些属性来创建LINQ to实体。
我已经看到了一些使用fluent API解决这个问题的解决方案。但我想知道是否有一个注释可以解决这个问题?