NHibernateでSQL Server 2005を使用してuintをマップする方法
-
11-07-2019 - |
質問
エンティティにuint型のプロパティがあります。次のようなもの:
public class Enity
{
public uint Count {get;set;}
}
それをSQL Server 2005データベースに永続化しようとすると、例外が発生します
DialectはDbType.UInt32をサポートしていません
これを回避する最も簡単な方法は何でしょうか。たとえば、DBに長く保存できます。 NHibernateにそれを伝える方法がわからないだけです。
解決
最もクリーンで、最も公式なソリューションは、おそらくユーザータイプを記述することです。
これを追加し、適応させます。多くの uint
がある場合は、ユーザータイプを用意する価値があります。
<property name="Prop" type="UIntUserType"/>
他のヒント
これを試していないので、これがうまくいくかどうかはわかりませんが、独自の方言を作成してweb.config / app.configに登録してみてください
方言クラス:
public class MyDialect:MsSql2005Dialect
{
public MyDialect()
{
RegisterColumnType(System.Data.DbType.UInt32, "bigint");
}
}
Web.config:
configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
<property name="connection.connection_string">
Server=127.0.0.1; Initial Catalog=thedatabase; Integrated Security=SSPI
</property>
<property name="dialect">MyDialect</property>
<property name="current_session_context_class">managed_web</property>
</session-factory>
</hibernate-configuration>
<!-- other app specific config follows -->
</configuration>
<property name="Prop" type="long"/>
別のプライベート「ミラー」プロパティーを追加しようとすることができます。
public class Enity
{
public uint Count {get;set;}
private long CountAsLong
{
get { return Convert.ToInt64(Count); }
set { Count = Convert.ToUInt(value); }
}
}
<property name="CountAsLong" type="long"/>
もちろん、これはマッピングによって解決できない場合にのみ 行う必要があります。
所属していません StackOverflow