簡體   English   中英

WebApi OData v4 ComlexType不能具有EntityType

[英]WebApi OData v4 ComlexType cannot have an EntityType

我們使用的是WebApi OData v 3,並且在實體數據模型中定義了一些包含EntitySet實體的ComplexType實體。

當移動到OData v 4並且在執行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 ,這些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; }

這是V4協議的一部分,OData Vib還沒有為OData V4實現(因此OData V4的Web API不支持定義這樣的模型,因為它基於ODataLib和其他核心庫)。 請在https://github.com/odata/odata.net/issues上打開一個Github問題來詢問它並幫助跟蹤它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM