【问题标题】:Error trying $expand on a nullable Navigation Property在可为空的导航属性上尝试 $expand 时出错
【发布时间】:2013-07-03 01:45:52
【问题描述】:

我正在使用最新的 ASP.NET 每晚构建。在我的 REST API(基于 EntitySetController)上,当我尝试 $expand 数据库中为空的导航属性时,我收到以下错误:

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

我的休息电话是:

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

【问题讨论】:

  • 我在 2 天内尝试了很多技巧,没有运气使用“DbContext.Configuration.ProxyCreationEnabled”设置...在“ODataEntityTypeSerializer”最新代码行 224 中,我看到有一个空检查,但我想知道为什么事情仍然失败。见这里:github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…

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


【解决方案1】:

这是一个已知问题 (1043: $expand fails when the navigation property being expanded is null)。我刚刚签入了fix。您能否在明天获取我们最新的夜间版本并确认问题是否已解决?

【讨论】:

  • 工作就像一个魅力.. 你从我们正在使用的一堆黑客中拯救了我们!继续努力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多