在可为null的导航属性上尝试$expand时出错
本文关键字:expand 出错 属性 null 导航 | 更新日期: 2023-09-27 18:21:10
我正在使用最新的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)
休息呼叫我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
更新:在使实体主键可以为null(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)
这是一个已知的问题(1043:$expand在被扩展的导航属性为null时失败)。我刚刚签入了对此的修复程序。你能在明天获取我们最新的夜间构建并确认问题是否已经解决吗?