我正在使用最新的 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)
休息电话 Im 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)