【问题标题】:WebApi OData v4 ComlexType cannot have an EntityTypeWebApi OData v4 ComlexType 不能有 EntityType
【发布时间】:2014-11-29 21:39:16
【问题描述】:

我们使用 WebApi OData v3 并在包含 EntitySet 实体的实体数据模型中定义了几个 ComplexType 实体。

当迁移到 OData v4 并执行 modelBulder.GetEdmModel() 步骤时,我们会收到 InvalidOperationException “复杂类型 'Foo' 通过属性 'Bar' 引用实体 'Bar'。”

我查看规范并看到一个称为“抽象实体类型”的东西,它是一种包含实体类型的类型。我在 WebApi OData 代码中没有看到这一点,所以我希望我所要做的就是声明一个没有密钥的 EntityType,然后我得到一个。

没有骰子。使我的 Foo 类型为 EntityType(使用 modelBuilder.AddEntityType(typeof(Foo)) 而不是 modelBuilder.AddComplexType(typeof(Foo)))会产生 InvalidOperationException “实体 'Foo' 没有定义键”。

是否有使用 ComplexTypeEntityType 的实体数据模型干净的方法?

一个干净但痛苦的解决方案是让更多本质上是我的实体的类重命名并作为ComplexType添加到模型中,以便我可以将数据作为复杂类型返回(我将包括来自实体的强制转换运算符类型到匹配的复杂类型,以便它们可以在客户端代码中互换使用)。显然,我不想因为在升级到 OData v4 之前运行良好的东西而经历这种痛苦。

一个更简单但不干净的方法(我已经这样做并且它有效)涉及将一个未使用的密钥插入到每个现在 EntityType 中,它曾经是一个 ComplexType,如下所示:

/// <summary>
/// Gets or sets the not used "key" property
/// </summary>
/// <remarks>
/// OData v4 seems to have broken the ability of a complex type to hold 
/// an entity type. In the spec, there is the notion of an abstract 
/// entity type - an entity type that does not have a key. But, it 
/// appears v4 doesn't support abstract entity types. Hence, this "key".
/// </remarks>
[Key]
public int NotUsed { get; set; }

【问题讨论】:

    标签: c# asp.net-web-api odata


    【解决方案1】:

    这是 V4 协议的一部分,尚未由 ODataLib for OData V4 实现(因此 OData V4 的 Web API 不支持定义此类模型,因为它基于 ODataLib 和其他核心库)。请在https://github.com/odata/odata.net/issues 上打开一个 Github 问题以请求它并帮助跟踪它。

    【讨论】:

    • 有什么链接可以关注吗?
    • 是否已添加对此的支持,或者是否存在可以跟踪的未解决问题?
    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2015-11-06
    • 2015-01-28
    • 2016-06-25
    • 2014-07-26
    • 2016-11-02
    • 1970-01-01
    相关资源
    最近更新 更多