SqlNullValueException:Json调用时数据为空

时间:2014-08-20 19:11:57

标签: c# sql json entity-framework

我已经看到了一些关于此问题的线索,但似乎没有一个在我的案例中有所帮助。我正在(使用EF)查询POCO对象,然后尝试通过MVC项目中的Json返回结果。

我的控制器返回一个JsonResult,我控制器的最后两行如下所示:

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

在调用Json()的第二行中发生错误。但是,我无法知道导致问题的财产。我在电话会议前查看了结果列表,一切看起来都很好。肯定有null属性,但没有必需的属性。

我将在下面包含错误输出(虽然它格式不正确)。究竟是什么导致了这个问题似乎并不十分明显 - 我怎样才能找出问题所在?

错误文字

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)

这是我的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 个答案:

答案 0 :(得分:5)

当JSON序列化程序尝试序列化Address.Location中的DbGeography实例时,可能会引发异常。该实例的某些(double - 值和延迟解析的)属性对于实例所代表的位置无效,但序列化程序并不知道并继续并尝试访问导致异常的属性

[ScriptIgnore]属性添加到Address.Location属性应修复它。