Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 在实体框架6中映射奇数关系_Entity Framework_Entity Framework 6_Relationships - Fatal编程技术网

Entity framework 在实体框架6中映射奇数关系

Entity framework 在实体框架6中映射奇数关系,entity-framework,entity-framework-6,relationships,Entity Framework,Entity Framework 6,Relationships,我有一套3个模型,这是一种奇怪的多对多关系 public class Metric { public int Id { get; set; } public string Name { get; set; } // ... } public class ActionPlan { public int Id { get; set; } public string Name { get; set; } public DateTime StartDat

我有一套3个模型,这是一种奇怪的多对多关系

public class Metric {
    public int Id { get; set; }
    public string Name { get; set; }
    // ...
}

public class ActionPlan {
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    //...

    public virtual ICollection<Metric> Metrics { get; set; }
}

public class PlanMetric {
    public int PlanId { get; set; }
    public int MetricId { get; set; }
    public decimal GoalValue { get; set; }

    public virtual ActionPlan Plan { get; set; }
    public virtual Metric Metric { get; set; }
}
公共类度量{
公共int Id{get;set;}
公共字符串名称{get;set;}
// ...
}
公开课行动计划{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间起始日期{get;set;}
//...
公共虚拟ICollection度量{get;set;}
}
公共类平面度量{
公共整数平面ID{get;set;}
公共int MetricId{get;set;}
公共十进制目标值{get;set;}
公共虚拟行动计划计划{get;set;}
公共虚拟度量{get;set;}
}
我将这些关系映射如下:

public class PlanMetricMapping : EntityTypeConfiguration<PlanMetric> {
    public PlanMetricMapping() {
        ToTable("PlanMetric");

        HasKey(m => new {
            m.MetricId,
            m.PlanId
        });

        Property(m => m.GoalValue)
            .IsRequired()
            .HasPrecision(10, 2);

        HasRequired(m => m.Metric)
            .WithMany()
            .HasForeignKey(m => m.MetricId);

        HasRequired(m => m.Plan)
            .WithMany()
            .HasForeignKey(m => m.PlanId);
    }
}

public class ActionPlanMapping : EntityTypeConfiguration<ActionPlan> {
    public ActionPlanMapping() {
        ToTable("ActionPlan");

        HasKey(m => m.Id);

        // ...

        //HasMany(m=>m.Metrics) // how do I get to this data?
    }
}
公共类PlanMetricMapping:EntityTypeConfiguration{
公共PlanMetricMapping(){
ToTable(“PlanMetric”);
HasKey(m=>new{
m、 MetricId,
m、 平面
});
属性(m=>m.GoalValue)
.IsRequired()
.精度(10,2);
HASSrequired(m=>m.Metric)
.有很多
.HasForeignKey(m=>m.MetricId);
HasRequired(m=>m.Plan)
.有很多
.HasForeignKey(m=>m.PlanId);
}
}
公共类ActionPlanMapping:EntityTypeConfiguration{
公共行动计划映射(){
ToTable(“行动计划”);
HasKey(m=>m.Id);
// ...
//HasMany(m=>m.Metrics)//如何获取这些数据?
}
}
问题是

1) EF正在我的
Metric
表中创建一个
ActionPlan\u Id
字段,我不知道为什么


2) 我不知道如何设置映射,以便能够从
计划
导航到其
指标

EF正在创建一个
行动计划Id
字段,因为您

public virtual ICollection<Metric> Metrics { get; set; }
相反


然后,为了获得
行动计划的指标,您可以通过
Select()
查看该集合

为什么要将
公共虚拟ICollection度量{get;set;}
放在
行动计划中?
public virtual ICollection<PlanMetric> PlanMetrics { get; set; }