简体   繁体   English

NHibernate按代码映射和IUsertype不起作用

[英]NHibernate mapping-by-code and IUsertype not working

I am trying to get a custom type working with NHibernate (v3.3) mapping by code. 我试图通过代码使用NHibernate(v3.3)映射的自定义类型。 I tried following this example here , but with no luck. 在这里尝试遵循此示例,但是没有运气。 The custom type I'm trying to achieve is a one that trims the strings coming from a database. 我试图实现的自定义类型是一种修剪来自数据库的字符串的类型。

I am getting the following exception: 我收到以下异常:

PropertyAccessException: Invalid Cast (check your mapping for property type mismatches). PropertyAccessException:无效的类型转换(检查映射是否存在属性类型不匹配)。 {"Unable to cast object of type 'System.String' to type 'ConsoleApplication1.TrimmedString'."} {“无法将类型为“ System.String”的对象转换为类型为“ ConsoleApplication1.TrimmedString”。”}

Here is my full attempt ( gist ). 这是我的完整尝试( gist )。

public class TrimmedString : IUserType
{
    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        //treat for the posibility of null values
        string resultString = (string) NHibernateUtil.String.NullSafeGet(rs, names[0]);
        if (resultString != null)
            return resultString.Trim();
        return null;
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        if (value == null)
        {
            NHibernateUtil.String.NullSafeSet(cmd, null, index);
            return;
        }

        value = ((string) value).Trim();

        NHibernateUtil.String.NullSafeSet(cmd, value, index);
    }

    public object DeepCopy(object value)
    {
        if (value == null) return null;
        return string.Copy((String) value);
    }

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

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

    public SqlType[] SqlTypes
    {
        get
        {
            SqlType[] types = new SqlType[1];
            types[0] = new SqlType(DbType.String);
            return types;
        }
    }

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

    public bool IsMutable
    {
        get { return false; }
    }

    public new bool Equals(object x, object y)
    {
        if (ReferenceEquals(x, y)) return true;

        var xString = x as string;
        var yString = y as string;
        if (xString == null || yString == null) return false;

        return xString.Equals(yString);
    }

    public int GetHashCode(object x)
    {
        return x.GetHashCode();
    }
}

Here is my mapping: 这是我的映射:

public class Person
{
    public virtual int Id { get; set; }
    public virtual TrimmedString FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class PersonMap : ClassMapping<Person>
{
    public PersonMap()
    {
        Table("Source");
        Id(i => i.Id);
        Property(i => i.FirstName, map => map.Type<TrimmedString>());
        Property(i => i.LastName);
    }
}

Not sure if I have to do anything special in the NHibernate configuration object, but I have included that in the Gist linked above. 不知道我是否必须在NHibernate配置对象中做任何特别的事情,但是我已经将其包含在上面链接的Gist中。

In Person , it should be... Person ,应该是...

public virtual string FirstName { get; set; }

..., not TrimmedString . ...,而不是TrimmedString TrimmedString is just the class that instructs NHibernate how you want that property to be hydrated and dehydrated. TrimmedString只是指示NHibernate如何使该属性水合和脱水的类。 The property it is applied to should be of the type specified by ReturnedType - in other words, String . 应用到的属性应为ReturnedType指定的类型,即String NHibernate is trying to set the FirstName property with a string value (because that's what the TrimmedString said it should do), but it can't because FirstName only allows TrimmedString s, hence the "Invalid Cast" error. NHibernate试图用string值设置FirstName属性(因为这就是TrimmedString所说的),但是不能这样做,因为FirstName只允许TrimmedString ,因此出现“ Invalid Cast”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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