我有两个相关实体(我添加了所有可能的数据注释,但似乎 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>
如您所见,它生成了两个不同的关联,而不是一个。如何解决?谢谢!