2013-07-05 72 views
1

我正在使用最新的ASP.NET每晚構建。在我的REST API(EntitySetController爲主),當我嘗試$展開導航屬性是空在數據庫中,我得到以下錯誤:

"message": "The EDM instance of type '[Communicator.Model.ContentTemplateGroup Nullable=True]' is missing the property 'GUID'.", 
"type": "System.InvalidOperationException", 
"stacktrace": " at System.Web.Http.OData.EntityInstanceContext.GetPropertyValue(String propertyName) 

REST調用林macking是:

/ContentTemplate?$expand=ContentTemplateGroup 

只有在數據庫中存在'ContentTemplateGroup'的ContentTemplate實例(空FK)時纔會發生這種情況。對於具有ContentTemplateGroup的ContentTemplates,$ expand可以正常工作。我的實體看起來像如下:

public class ContentTemplate: IIdentifier 
{ 
    public int? Id { get; set; } 

    [Required] 
    public Guid GUID { get; set; } 

    public virtual ContentTemplateGroup ContentTemplateGroup { get; set; } 
    public virtual ICollection<ContentTemplateField> ContentTemplateFields { get; set; } 
} 

public class ContentTemplateGroup : IIdentifier 
{ 
    public int? Id { get; set; } 

    [Required] 
    public Guid GUID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    public virtual IList<ContentTemplate> ContentTemplate { get; set; } 
} 

,當我嘗試使用集合擴大導航屬性我沒有得到這個錯誤。以下的工作就像一個魅力:

http://localhost:64316/rest/ContentTemplate?$expand=ContentTemplateFields 

更新:(?INT)使得實體主鍵爲空的之前,我得到一個不同的錯誤進行了同樣的問題情境。錯誤是:

"message": "The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.", 
"type": "System.InvalidOperationException", 
"stacktrace": " at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal)\r\n at lambda_method(Closure , Shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\n at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, ODataWriter writer, ODataSerializerContext writeContext) 
+0

我已經在2天內嘗試了很多技巧,玩'DbContext.Configuration.ProxyCreationEnabled'設置沒有運氣......在'ODataEntityTypeSerializer'最新的代碼行224我看到有一個檢查null,但我想知道爲什麼事情仍然失敗。看到這裏:https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataEntityTypeSerializer.cs – Hasith

回答