関連エンティティが 2 つあります (考えられるすべてのデータ注釈を追加しましたが、ODataConventionModelBuilder はそれらを無視しているようです)。
public class Examination
{
[InverseProperty("Examination")]
public virtual ICollection<Variable> Variables { get; set; }
}
public abstract class Variable
{
[Required]
public int? ExaminationId { get; set; }
[Required]
[ForeignKey("ExaminationId")]
[InverseProperty("Variables")]
public virtual Examination Examination { get; set; }
}
これは OData モデルの生成です (HasMany() と HasRequired() は結果のモデルに影響しないようです):
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Examination>("Examinations");
modelBuilder.EntitySet<Variable>("Variables");
modelBuilder.Entity<Examination>().HasMany(e => e.Variables);
modelBuilder.Entity<Variable>().HasRequired(v => v.Examination);
return modelBuilder.GetEdmModel();
結果のモデル:
<EntityType Name="Examination">
<NavigationProperty Name="Variables" Relationship="YoStat.Models.YoStat_Models_Examination_Variables_YoStat_Models_Variable_VariablesPartner" ToRole="Variables" FromRole="VariablesPartner"/>
</EntityType>
<EntityType Name="Variable" Abstract="true">
<NavigationProperty Name="Examination" Relationship="YoStat.Models.YoStat_Models_Variable_Examination_YoStat_Models_Examination_ExaminationPartner" ToRole="Examination" FromRole="ExaminationPartner"/>
</EntityType>
<Association Name="YoStat_Models_Examination_Variables_YoStat_Models_Variable_VariablesPartner">
<End Type="YoStat.Models.Variable" Role="Variables" Multiplicity="*"/>
<End Type="YoStat.Models.Examination" Role="VariablesPartner" Multiplicity="0..1"/>
</Association>
<Association Name="YoStat_Models_Variable_Examination_YoStat_Models_Examination_ExaminationPartner">
<End Type="YoStat.Models.Examination" Role="Examination" Multiplicity="1"/>
<End Type="YoStat.Models.Variable" Role="ExaminationPartner" Multiplicity="0..1"/>
</Association>
<EntityContainer Name="Container">
<EntitySet Name="Examinations" EntityType="YoStat.Models.Examination"/>
<EntitySet Name="Variables" EntityType="YoStat.Models.Variable"/>
<AssociationSet Name="YoStat_Models_Examination_Variables_YoStat_Models_Variable_VariablesPartnerSet" Association="YoStat.Models.YoStat_Models_Examination_Variables_YoStat_Models_Variable_VariablesPartner">
<End Role="VariablesPartner" EntitySet="Examinations"/>
<End Role="Variables" EntitySet="Variables"/>
</AssociationSet>
<AssociationSet Name="YoStat_Models_Variable_Examination_YoStat_Models_Examination_ExaminationPartnerSet" Association="YoStat.Models.YoStat_Models_Variable_Examination_YoStat_Models_Examination_ExaminationPartner">
<End Role="ExaminationPartner" EntitySet="Variables"/>
<End Role="Examination" EntitySet="Examinations"/>
</AssociationSet>
</EntityContainer>
ご覧のとおり、1 つではなく 2 つの異なる関連付けが生成されます。修正方法は?ありがとう!