【问题标题】:How can I retrieve a Dialect instance in Fluent NHibernate mapping如何在 Fluent NHibernate 映射中检索方言实例
【发布时间】: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


    【解决方案1】:

    ISessionFactoryImplementor,看https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Engine/ISessionFactoryImplementor.cs

    Dialect.Dialect Dialect { get; }
    

    可能会使用:

    ISessionFactory factory = ...; 
    ((ISessionFactoryImplementor)factory)
    

    【讨论】:

    • 当然我可以从ISessionImplementor 得到一个Dialect 实例。但问题是我怎样才能在我的 FNH 映射中得到一个。我没有找到ISessionImplementor 的方法。
    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多