0

我有两个相关实体(我添加了所有可能的数据注释,但似乎 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>

如您所见,它生成了两个不同的关联,而不是一个。如何解决?谢谢!

4

1 回答 1

1

这看起来像 odata 模型构建器中的一个错误,它总是为每个导航属性创建单向导航,而更好的方法是查看模型以首先检查它是否是双向导航。我在以下位置提交了错误:http ://aspnetwebstack.codeplex.com/workitem/623

但是,我没有发现当前行为在客户端出现任何问题。WCF DS 客户端仍将生成正确的代理类。您是否有任何特定的用户场景被它阻止?这些信息将有助于更好地对错误进行分类。谢谢。

于 2012-11-12T22:19:54.543 回答