简体   繁体   中英

Map a NULL value from database to Property of object

I have a table in SQL server with some column (tinyint) with Null value like so:

在此处输入图片说明

The corresponding DataObject in C# with the Nullable values (burstHierarchyTypeId map on tbh_id and burstPlateformTypeId map on tbp_id):

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; }
}
}

I have a query that get the ligns of my table according to the ComId.

When I execute the query I get the error :

Invalid cast from 'System.Byte' to 'System.Nullable`1

here is the Setter (from PropertyUtils.cs):

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 + "'");
}
}

Error message when trying to set value of BurstPlateformTypeId (tbp_id = 1):

  Invalid cast from 'System.Byte' to 'System.Nullable`1

The Convert.ChangeTpe is from the metadata of C#, What I understood is the value get by the query is "1" so it detects an integer, but when it check the type of the property from the object it gets a nullable.

Why my property value (1) is consider Byte and not integer ? How can I map the NUll into the corresponding property (BurstPlateformTypeId) Null ?

After Further Investigation :

A tinyInt in SQL is consider byte in C# so my dataobject was false.

The solution in 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM