【发布时间】:2023-03-25 18:31:02
【问题描述】:
假设我们有一个使用 Fluent NHibernate 访问一些数据库的应用程序(我们不知道它到底是什么类型的数据库)。并考虑我们想要映射一些具有无符号类型 id 的类(例如 ulong):
public class MyClass
{
public ulong id { get; set; }
//Other stuff
...
}
我们还有一个自定义的IUserType 将 ulong 映射为 DB 支持的某种类型(例如 long)。让我们称之为ULongAsLong。
我的目标是编写一个MyClass 映射,以便如果DB 支持无符号类型,它的id 将映射为unsigned bigint,如果不支持,则映射为ULongAsLong。像这样的:
public class MyClassMap : ClassMap<MyClass>
{
public MyClassMap()
{
//Here goes some function or something that retrieves a Dialect instance.
var dialect = ...;
if (supportsULong(dialect))
{
Id(x => x.id);
}
else
{
Id(x => x.id).CustomType<ULongAsLong>();
}
//Mapping everything else
...
}
private bool supportsULong(Dialect dialect)
{
//Some code that finds out if ulong is supported by DB.
}
}
所以问题是如何在映射中检索 Dialect 实例以决定是将 id 映射为 ulong 还是 ULongAsLong。
【问题讨论】:
标签: nhibernate orm fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping