Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将空值从数据库映射到对象的属性_C#_Sql Server_Mapping - Fatal编程技术网

C# 将空值从数据库映射到对象的属性

C# 将空值从数据库映射到对象的属性,c#,sql-server,mapping,C#,Sql Server,Mapping,我在SQL server中有一个表,其中的某个列tinyint的值为Null,如下所示: C中对应的数据对象,其tbh_id上的burstHierarchyTypeId映射值为空,tbp_id上的burstPlateformTypeId映射值为空: public class BurstShortCutDo : BaseDomain { private long _adfId = ValueTypeUtils.Instance.LongNull; private string _websites

我在SQL server中有一个表,其中的某个列tinyint的值为Null,如下所示:

C中对应的数据对象,其tbh_id上的burstHierarchyTypeId映射值为空,tbp_id上的burstPlateformTypeId映射值为空:

public class BurstShortCutDo : BaseDomain
{

private long _adfId = ValueTypeUtils.Instance.LongNull;
private string _websitesIds;
private string _shortCutValue = "";
private int? _burstHierarchyTypeId = null;
private int? _burstPlateformTypeId = null;


#region CONSTRUCTORS

public BurstShortCutDo()
{
}
#endregion

#region PROPERTIES
public long AdfId
{
    get { return _adfId; }

    set { _adfId = value; }
}

public int? BurstHierarchyTypeId
{
    get { return _burstHierarchyTypeId; }
    set { _burstHierarchyTypeId = value; }
}

public int? BurstPlateformTypeId
{
    get { return _burstPlateformTypeId; }
    set { _burstPlateformTypeId = value; }
}

public string ShortCutValue
{
    get { return _shortCutValue; }
    set { _shortCutValue = value; }
}
}
我有一个查询,可以根据ComId获取表的对齐

当我执行查询时,我得到一个错误:

Invalid cast from 'System.Byte' to 'System.Nullable`1
以下是PropertyUtils.cs中的Setter:

private void SetPropertySimpleName(object obj, string propName, object propValue)
{
PropertyInfo propInfo = obj.GetType().GetProperty(propName);
if (propInfo != null && propInfo.CanWrite)
{
    if (propValue == null && !propInfo.PropertyType.IsValueType)
    {
        propInfo.SetValue(obj, null, null);
    }
    else if (propInfo.PropertyType.IsAssignableFrom(propValue.GetType()))
    {
        propInfo.SetValue(obj, propValue, null);
    }
    else if (propValue is IConvertible)
    {
        // CRASH HERE 
        propInfo.SetValue(obj, Convert.ChangeType(propValue, propInfo.PropertyType, CultureInfo.CurrentCulture), null);
    }
}
else 
{
    throw new ObjectNotFoundException("The property '" + propName + "' is not found in class '" + obj.GetType().FullName + "'");
}
}
尝试设置BurstPlateformTypeId tbp_id=1的值时出现错误消息:

  Invalid cast from 'System.Byte' to 'System.Nullable`1
Convert.ChangeTpe来自C的元数据,我理解的是查询得到的值是1,因此它检测到一个整数,但是当它从对象中检查属性类型时,它得到一个可为null的值

为什么我的属性值1是字节,而不是整数?
如何将NUll映射到相应的属性BurstPlateformTypeId NUll?

进一步调查后:

SQL中的TyyIt在C中考虑字节,所以我的DATAObjor是false。< /P> my dataobject中的解决方案:

private byte? _burstHierarchyTypeId = null;
private byte? _burstPlateformTypeId = null;

public byte? BurstHierarchyTypeId
{
   get { return _burstHierarchyTypeId; }
   set { _burstHierarchyTypeId = value; }
}

public byte? BurstPlateformTypeId
{
   get { return _burstPlateformTypeId; }
   set { _burstPlateformTypeId = value; }
}

你考虑过使用ORM吗?现在我不能,我使用的是一个需要深度重构的旧应用程序,没有服务或ORM。但是我发现了一个错误,在我的对象中,它不是整数而是字节。