FluentNHibernate 异常:"Property mapping has wrong number of columns" 复合用户类型
FluentNHibernate Exception: "Property mapping has wrong number of columns" on composite user type
我正在尝试将域模型中的一组实体映射到数据库中以逗号分隔的 ID 字符串。我已经编写了一个复合用户类型来为我处理这个问题:
public class CustomerSetToCommaString : ICompositeUserType
{
public object GetPropertyValue(object component, int property)
{
throw new NotImplementedException();
}
public void SetPropertyValue(object component, int property, object value)
{
throw new NotImplementedException();
}
public bool Equals(object x, object y)
{
var set1 = x as ISet<Customer>;
var set2 = x as ISet<Customer>;
return (x == null) ? y == null : set1.SetEquals(set2);
}
public int GetHashCode(object x)
{
var set = x as ISet<Customer>;
unchecked
{
return set == null ? 0 : set.Sum(relatie => relatie.GetHashCode());
}
}
public object NullSafeGet(DbDataReader dr, string[] names, ISessionImplementor session, object owner)
{
var str = (string)NHibernateUtil.String.Get(dr, names[0], session);
IList<int> ids = str.Split(',').Select(id => int.Parse(id.Trim())).ToList();
return ((ISession) session).QueryOver<Customer>().WhereRestrictionOn(customer => customer.Id).IsInG(ids).List()
.ToHashSet();
}
public void NullSafeSet(DbCommand cmd, object value, int index, bool[] settable, ISessionImplementor session)
{
var set = value as ISet<Customer>;
NHibernateUtil.String.Set(cmd, string.Join(", ", set.Select(customer => customer.Id.ToString()).ToArray()), index, session);
}
public object DeepCopy(object value)
{
return ((ISet<Customer>) value).ToHashSet();
}
public object Disassemble(object value, ISessionImplementor session)
{
return DeepCopy(value);
}
public object Assemble(object cached, ISessionImplementor session, object owner)
{
return DeepCopy(cached);
}
public object Replace(object original, object target, ISessionImplementor session, object owner)
{
return original;
}
public string[] PropertyNames => new string[0];
public IType[] PropertyTypes => new IType[0];
public Type ReturnedClass => typeof(ISet<Customer>);
public bool IsMutable => true;
}
然而,当尝试使用
Map(x => x.Customers).CustomType<CustomerSetToCommaString>.Column("Customers");
我发现 属性 映射的列数错误。它不应该尝试映射到单个列吗?我错过了什么吗?
经过一天令人沮丧的调试和 Google 搜索后,我找到了答案。问题出在这一行:
public IType[] PropertyTypes => new IType[0];
这指定了需要填写的列。当我将其更改为:
public IType[] PropertyTypes => new IType[1]{NHibernateUtil.String};
我指定该列是单个字符串,这解决了问题。
我正在尝试将域模型中的一组实体映射到数据库中以逗号分隔的 ID 字符串。我已经编写了一个复合用户类型来为我处理这个问题:
public class CustomerSetToCommaString : ICompositeUserType
{
public object GetPropertyValue(object component, int property)
{
throw new NotImplementedException();
}
public void SetPropertyValue(object component, int property, object value)
{
throw new NotImplementedException();
}
public bool Equals(object x, object y)
{
var set1 = x as ISet<Customer>;
var set2 = x as ISet<Customer>;
return (x == null) ? y == null : set1.SetEquals(set2);
}
public int GetHashCode(object x)
{
var set = x as ISet<Customer>;
unchecked
{
return set == null ? 0 : set.Sum(relatie => relatie.GetHashCode());
}
}
public object NullSafeGet(DbDataReader dr, string[] names, ISessionImplementor session, object owner)
{
var str = (string)NHibernateUtil.String.Get(dr, names[0], session);
IList<int> ids = str.Split(',').Select(id => int.Parse(id.Trim())).ToList();
return ((ISession) session).QueryOver<Customer>().WhereRestrictionOn(customer => customer.Id).IsInG(ids).List()
.ToHashSet();
}
public void NullSafeSet(DbCommand cmd, object value, int index, bool[] settable, ISessionImplementor session)
{
var set = value as ISet<Customer>;
NHibernateUtil.String.Set(cmd, string.Join(", ", set.Select(customer => customer.Id.ToString()).ToArray()), index, session);
}
public object DeepCopy(object value)
{
return ((ISet<Customer>) value).ToHashSet();
}
public object Disassemble(object value, ISessionImplementor session)
{
return DeepCopy(value);
}
public object Assemble(object cached, ISessionImplementor session, object owner)
{
return DeepCopy(cached);
}
public object Replace(object original, object target, ISessionImplementor session, object owner)
{
return original;
}
public string[] PropertyNames => new string[0];
public IType[] PropertyTypes => new IType[0];
public Type ReturnedClass => typeof(ISet<Customer>);
public bool IsMutable => true;
}
然而,当尝试使用
Map(x => x.Customers).CustomType<CustomerSetToCommaString>.Column("Customers");
我发现 属性 映射的列数错误。它不应该尝试映射到单个列吗?我错过了什么吗?
经过一天令人沮丧的调试和 Google 搜索后,我找到了答案。问题出在这一行:
public IType[] PropertyTypes => new IType[0];
这指定了需要填写的列。当我将其更改为:
public IType[] PropertyTypes => new IType[1]{NHibernateUtil.String};
我指定该列是单个字符串,这解决了问题。