在可空的导航属性上尝试$ expand时出错

时间:2013-07-05 03:34:56

标签: c# asp.net-mvc asp.net-mvc-5 odata

我正在使用最新的ASP.NET nightly版本。在我的REST API(基于EntitySetController)上,当我尝试$扩展数据库中为null的导航属性时,我收到以下错误:

"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)

休息电话Im macking是:

/ContentTemplate?$expand=ContentTemplateGroup

仅当存在数据库中存在“ContentTemplateGroup”的“ContentTemplate”实例(null 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

更新:在使实体主键为nullable(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)

1 个答案:

答案 0 :(得分:3)

这是一个已知问题(1043: $expand fails when the navigation property being expanded is null)。我刚刚检查了fix。你明天可以抓住我们最新的夜间制作并确认问题是否已经消失了吗?