Как сделать целочисленное значение нулевым через NHibernate - PullRequest
3 голосов
/ 10 мая 2011

У меня есть столбец INTEGER, который можно обнулить, и он сопоставлен со свойством int.Как я могу изменить это значение на ноль через NHibernate?int не может быть обнулен.

Ответы [ 2 ]

5 голосов
/ 10 мая 2011

используйте Nullable типа int? InSad int, а затем вы можете сопоставить его с NULL-столбцом в базе данных.

0 голосов
/ 12 мая 2015

Необходимо создать карту (m => m.EmployeeId) .CustomType (typeof (IntConvertToNullInt));

открытый класс IntConvertToNullInt: IUserType {public SqlType [] SqlTypes {get {return new [] {SqlTypeFactory.Int32};}}

    public Type ReturnedType
    {
        get { return typeof(Int32); }
    }

    public bool IsMutable
    {
        get { return false; }
    }

    public int GetHashCode(object x)
    {
        if (x == null)
        {
            return 0;
        }
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        object obj = NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
        if (obj == null)
        {
            return 0;
        }
        return Convert.ToInt32(obj);
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {

        if (value == null)
        {
            NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
            return;
        }
        else
        {
            int indexint = (int)value;
            if (indexint == 0)
            {
                NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
            }
            else
            {
                NHibernateUtil.Int32.NullSafeSet(cmd, value, index);
            }
        }            

    }

    public object DeepCopy(object value)
    {
        return value;
    }

    public object Replace(object original, object target, object owner)
    {
        return original;
    }

    public object Assemble(object cached, object owner)
    {
        return cached;
    }

    public object Disassemble(object value)
    {
        return value;
    }

    bool IUserType.Equals(object x, object y)
    {
        return object.Equals(x, y);
    }
...