如何创建/使用Fluent NHibernate约定将UInt32属性自动映射到SQL Server 2008数据库?
我试图使用约定将UInt32属性映射到SQL Server 2008数据库。由于Fluent NHibernate工作方式的更新,我似乎无法基于现有web源创建解决方案,即示例已经过时 我试图让NHibernate生成模式(通过ExposeConfiguration)。我很高兴NHibernate将其映射到任何合理的东西(例如bigint) 以下是我目前的代码(当我尝试公开模式时,由于SQL Server不支持UInt32,代码失败)。抱歉代码有点长,但我不能100%确定什么与问题有关,所以我在谨慎方面犯了错误。大部分都是基于 报告的错误是:如何创建/使用Fluent NHibernate约定将UInt32属性自动映射到SQL Server 2008数据库?,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我试图使用约定将UInt32属性映射到SQL Server 2008数据库。由于Fluent NHibernate工作方式的更新,我似乎无法基于现有web源创建解决方案,即示例已经过时 我试图让NHibernate生成模式(通过ExposeConfiguration)。我很高兴NHibernate将其映射到任何合理的东西(例如bigint) 以下是我目前的代码(当我尝试公开模式时,由于SQL Server不支持UInt32,代码失败)。抱歉代码有点长,但我不能100%确定什么与问题有关,所以我在
System.ArgumentException : Dialect does not support DbType.UInt32
我想我需要一个相对全面的例子,因为目前我似乎无法将各个部分整合成一个有效的解决方案
FluentConfiguration configuration =
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(connectionString))
.Mappings(mapping =>
mapping.AutoMappings.Add(
AutoMap.AssemblyOf<Product>()
.Conventions.Add<UInt32UserTypeConvention>()));
configuration.ExposeConfiguration(x => new SchemaExport(x).Create(false, true));
namespace NHibernateTest
{
public class UInt32UserTypeConvention : UserTypeConvention<UInt32UserType>
{
// Empty.
}
}
namespace NHibernateTest
{
public class UInt32UserType : IUserType
{
// Public properties.
public bool IsMutable
{
get
{
return false;
}
}
public Type ReturnedType
{
get
{
return typeof(UInt32);
}
}
public SqlType[] SqlTypes
{
get
{
return
new SqlType[]
{
SqlTypeFactory.Int32
};
}
}
// Public methods.
public object Assemble(object cached, object owner)
{
return cached;
}
public object DeepCopy(object value)
{
return value;
}
public object Disassemble(object value)
{
return value;
}
public new bool Equals(object x, object y)
{
return (x != null && x.Equals(y));
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
int? i = (int?)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
return (UInt32?)i;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
UInt32? u = (UInt32?)value;
int? i = (Int32?)u;
NHibernateUtil.Int32.NullSafeSet(cmd, i, index);
}
public object Replace(object original, object target, object owner)
{
return original;
}
}
}
FluentConfiguration配置=
fluent.Configure()
.数据库(MsSqlConfiguration.MsSql2008
.连接字符串(连接字符串))
.Mappings(映射=>
mapping.AutoMappings.Add(
自动映射
.Conventions.Add());
ExposeConfiguration(x=>newschemaexport(x).Create(false,true));
名称空间NHibernateTest
{
公共类UINT32 UserTypeConvention:UserTypeConvention
{
//空的。
}
}
名称空间NHibernateTest
{
公共类UINT32用户类型:IUserType
{
//公共财产。
公共布尔可换
{
得到
{
返回false;
}
}
公共类型返回类型
{
得到
{
返回类型(UInt32);
}
}
公共SqlType[]SqlTypes
{
得到
{
返回
新的SqlType[]
{
SqlTypeFactory.Int32
};
}
}
//公共方法。
公共对象集合(对象缓存,对象所有者)
{
返回缓存;
}
公共对象深度复制(对象值)
{
返回值;
}
公共对象(对象值)
{
返回值;
}
公共新布尔等于(对象x、对象y)
{
返回(x!=null&&x.Equals(y));
}
公共int GetHashCode(对象x)
{
返回x.GetHashCode();
}
公共对象NullSafeGet(IDataReader rs,字符串[]名称,对象所有者)
{
int?i=(int?)NHibernateUtil.Int32.NullSafeGet(rs,names[0]);
返回(UInt32?)i;
}
public void NullSafeSet(IDbCommand cmd,对象值,int索引)
{
UInt32?u=(UInt32?)值;
int?i=(Int32?)u;
NHibernateUtil.Int32.NullSafeSet(cmd,i,index);
}
公共对象替换(对象原始、对象目标、对象所有者)
{
归还原件;
}
}
}
当然,您需要映射到现有的SQL Server数据类型
基于此问题,您可以选择:
- CLR数据类型
bigint
decimal
- 使用int.MinValue映射到
int