SqlNullValueException: Json调用的数据为空

时间:2022-03-11 02:14:47

I've seen a few threads regarding this but none that seems to help in my case. I am querying (with EF) into POCO objects then trying to return the results via Json in an MVC project.

我已经见过一些关于这方面的文章,但没有一篇似乎对我的文章有帮助。我正在(用EF)查询POCO对象,然后尝试在MVC项目中通过Json返回结果。

My controller returns a JsonResult and the last 2 lines of my controller look like this:

我的控制器返回一个JsonResult,我的控制器的最后两行是这样的:

var results = _context.Churches.Include(c => c.Address).Include(c => c.Address.Country).Where(predicate).ToList();
return Json(results, JsonRequestBehavior.AllowGet);

The error occurs in the second line on the call to Json(). However, I have no way of knowing what property is causing the issue. I've looked at the results list before the call and everything looks fine. There are definitely null properties but none that are Required.

错误发生在调用Json()的第二行。然而,我无法知道是什么属性导致了这个问题。我在电话前看过结果列表,一切看起来都很好。肯定有空属性,但不需要。

I'll include the error output below (although its doesn't format well). It doesn't seem very apparent what exactly is causing the problem -- how can I figure out where the issue lies?

我将包括下面的错误输出(尽管它的格式不太好)。这似乎不太清楚到底是什么导致了这个问题——我怎么才能知道问题在哪里?

Error Text

错误的文本

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at System.Data.SqlTypes.SqlDouble.get_Value()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
at System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

and here is my POCO model:

这是我的POCO模型:

    public class Church : IUserTrackingEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Display(Name="Denomination")]
    public int? DenominationId { get; set; }
    public Denomination Denomination { get; set; }
    [Display(Name="Web Address")]
    public string WebAddress { get; set; }
    public string Contact { get; set; }
    [Display(Name="Position")]
    public int? ContactPositionId { get; set; }
    public ContactPosition ContactPosition { get; set; }
    [Display(Name="Email")]
    public string EmailAddress { get; set; }
    [Display(Name="Phone Number")]
    public string PhoneNumber { get; set; }
    [Display(Name="Ext.")]
    public string PhoneExtension { get; set; }
    [Display(Name="Phone Type")]
    public int? PhoneNumberTypeId { get; set; }
    [Display(Name = "Country")]
    public int? CountryId { get; set; }
    public Country Country { get; set; }
    [Display(Name = "Partnership Requested")]
    public DateTime? PartnershipRequestDate { get; set; }
    [Display(Name = "Partnership Accepted")]
    public DateTime? PartnershipAcceptDate { get; set; }
    [Display(Name = "Last Contacted")]
    public DateTime? LastContactDate { get; set; }
    public string Notes { get; set; }
    public string LastChangedBy { get; set; }
    public int? AddressId { get; set; }
    public Address Address { get; set; }
}
    public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Continent { get; set; }
    public string PostalCodeFormat { get; set; }
    public string PostalCodeRegex { get; set; }
    public string Citizenship { get; set; }
    public string NTMCode { get; set; }
    public string ISOCode { get; set; }
    public string ISO3Code { get; set; }
    public string FIPSCode { get; set; }
}
    public class Address : IUserTrackingEntity
{
    public int Id { get; set; }
    public string Addressee { get; set; }
    [Required(ErrorMessage = "A value must be entered into street 1.")]
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string Locality { get; set; }
    public string District { get; set; }
    [Required(ErrorMessage="A Country is required.")]
    public int CountryId { get; set; }
    public Country Country { get; set; }
    public string PostalCode { get; set; }
    public bool IsVerified { get; set; }
    public string NTMId { get; set; }
    public string LastChangedBy { get; set; }
    public DbGeography Location { get; set; }

    public double? Longitude
    {
        get
        {
            return Location == null ? null : Location.Longitude;
        }

        set
        {
            if (value != null)
                Location = DbGeography.FromText(String.Format("POINT({0} {1})", value, Latitude ?? 0d), 4326);
        }
    }
    public double? Latitude
    {
        get
        {
            return Location == null ? null : Location.Latitude;
        }

        set
        {
            if (value != null)
                Location = DbGeography.FromText(String.Format("POINT({0} {1})", Longitude ?? 0d, value), 4326);
        }
    }

}

1 个解决方案

#1


5  

The exception is probably being thrown when the JSON serializer attempts to serialize the DbGeography instance in Address.Location. Some (double-valued and lazily resolved) property of that instance is not valid for the location the instance represents, but the serializer doesn't know that and goes ahead and tries to access that property which causes the exception.

当JSON序列化器试图在Address.Location中序列化DbGeography实例时,可能会抛出异常。该实例的某些(双值和延迟解析)属性对于实例表示的位置无效,但是序列化器不知道这一点,并继续访问导致异常的属性。

Adding a [ScriptIgnore] attribute to the Address.Location property should fix it.

向地址添加[ScriptIgnore]属性。Location属性应该修复它。

#1


5  

The exception is probably being thrown when the JSON serializer attempts to serialize the DbGeography instance in Address.Location. Some (double-valued and lazily resolved) property of that instance is not valid for the location the instance represents, but the serializer doesn't know that and goes ahead and tries to access that property which causes the exception.

当JSON序列化器试图在Address.Location中序列化DbGeography实例时,可能会抛出异常。该实例的某些(双值和延迟解析)属性对于实例表示的位置无效,但是序列化器不知道这一点,并继续访问导致异常的属性。

Adding a [ScriptIgnore] attribute to the Address.Location property should fix it.

向地址添加[ScriptIgnore]属性。Location属性应该修复它。