代码优先,难以映射简单的复合键连接表

时间:2013-12-10 04:26:47

标签: asp.net asp.net-mvc

请帮助,让小猫在这里尝试做一些应该简单的事情。我已经掌握了1-1和1-M,但是这个>我只是想实现复合键,即PremiseServiceSubscription充当具有2个外键加上其自己的主键的连接表。我希望能够键入Premise.PremiseServiceSubscription来获取该Premise的所有服务的集合。我应该如何在此处表示并使用Fluent API?

public class Premise
{
    public int CustomerId { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }

    public virtual ICollection<PremiseServiceSubscription> PremiseServiceSubscriptions { get; set; } 


}

public class PremiseService
{
    public int PremiseServiceId { get; set; }
    public string PremiseServiceDescription {get; set;}


}

public class PremiseServiceSubscription
{
    public int PremiseServiceSubscriptionId { get; set; }
    public int CustomerId { get; set; }
    public int PremiseServiceId { get; set; }

}

1 个答案:

答案 0 :(得分:0)

立即工作......

[关键]         public int SupplyPointServiceSubscriptionId {get;组; }

    [Column("SupplyPointId")]
    public int SupplyPointId { get; set; }
    [Column("SupplyPointServiceId")]
    public int SupplyPointServiceId { get; set; }
    public  DateTime EffectiveDateTime { get; set; }

使用流畅的API

        modelBuilder.Entity<SupplyPointServiceSubscription>()
            .HasKey(p => new { p.SupplyPointId, p.SupplyPointServiceId });