[英]EF6 Fluent API Map Related Entities On Custom Key/Properties Not PK
我有以下型號
public class Rate
{
public Guid Id { get; set; } // PK
public int SpecialId { get; set; }
[Column(TypeName = "Money")]
public decimal Rental { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
還有以下...
public class Vehicle
{
public Guid Id { get; set; } // PK
public int SpecialId { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
// List of rates on
public virtual ICollection<Rate> Rates { get; set; }
}
因此,“費率”將具有車輛,而“車輛”可能具有費率列表。 通常我只使用PK和EF進行映射,效果很好。
但是在這種情況下,我需要使用SpecialId映射/加入它們。 我從“速率映射”類的以下內容開始,但是只能映射連接的一側。 我如何告訴雙方不要使用PK ID,而要使用SpecialId?
HasKey(x => x.Id);
Property(x => x.Id).IsRequired();
Property(x => x.SpecialId).IsRequired();
HasOptional(x => x.Vehicle).WithMany(x => x.Rates).Map(m => m.MapKey("SpecialId"));
是否可以在Rate和Vehicle表上同時使用SpecialId來將兩者結合在一起。 可能是該.Map()方法的另一個重載?
我認為您所需的內容在EF 6.x和早期版本中不受支持,但在EF 7中被稱為Alternate Key 。 您的情況是:
Entity<Rate>().Reference(r=>r.Vehicle)
.InverseCollection(v=>v.Rates)
.ForeignKey(r=>r.SpecialId)
.PrincipalForeignKey(v=>v.SpecialId);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.