gpt4 book ai didi

c# - 如何在 MySQL 服务器和 Microsoft SQL Server 2008 中使用 NHibernate

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:05 24 4
gpt4 key购买 nike

如何配置 NHibernate 以连接到 MySQL 服务器和 Microsoft SQL Server 2008?我确实想将数据从一台服务器复制到另一台服务器。我听说 NHibernate 共享。

最佳答案

几个月前我一直在挣扎。我的问题出在 MS Sql Server 和 Oracle 上。

我所做的是为 nhibernate 创建两个单独的配置文件:

sql.nhibernate.config

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<reflection-optimizer use="false" />
<session-factory name="BpSpedizioni.MsSql">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<!-- <property name="connection.connection_string">Data Source=(local); Initial Catalog=NHibernate; Trusted_Connection=true;</property> -->
<property name="current_session_context_class">web</property>
<property name="adonet.batch_size">100</property>
<property name="command_timeout">120</property>
<property name="max_fetch_depth">3</property>
<property name='prepare_sql'>true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="BpSpedizioni.Services"/>
</session-factory>
</hibernate-configuration>

ora.nhibernate.config

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<reflection-optimizer use="false" />
<session-factory name="BpSpedizioni.Oracle">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<!-- <property name="connection.connection_string">Data Source=(local); Initial Catalog=NHibernate; Trusted_Connection=true;</property> -->
<property name="current_session_context_class">web</property>
<property name="adonet.batch_size">100</property>
<property name="command_timeout">120</property>
<property name="max_fetch_depth">3</property>
<property name='prepare_sql'>true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="BpSpedizioni.Services"/>
</session-factory>
</hibernate-configuration>

我使用这个简单的类来构建我的 nhibernate SessionFactory:

    public class NHibernateSessionFactory
{
private ISessionFactory sessionFactory;

private readonly string ConnectionString = "";
private readonly string nHibernateConfigFile = "";

public NHibernateSessionFactory(String connectionString, string nHConfigFile)
{
this.ConnectionString = connectionString;
this.nHibernateConfigFile = nHConfigFile;
}

public ISessionFactory SessionFactory
{
get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); }
}

private ISessionFactory CreateSessionFactory()
{
Configuration cfg;
cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.nHibernateConfigFile));

// With this row below Nhibernate searches for the connection string inside the App.Config.
// cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, this.ConnectionString);

#if DEBUG
cfg.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true");
cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true");
#endif

return (cfg.BuildSessionFactory());
}
}

如您所见,我向我的 NHibernateSessionFactory 传递了一个连接字符串(我更喜欢将其保存在我的应用程序配置文件中)和 nhibernate 配置文件的名称(没有路径)。

我个人使用 DI 容器 (StructureMap),您可以通过定义注册表类实现非常酷的效果:

public class NhibernateRegistry : Registry
{
public NhibernateRegistry()
{

For<ISessionFactory>()
.Singleton()
.Add(new NHibernateSessionFactory(<oracle connection string>, "ora.nhibernate.config").SessionFactory)
.Named("OracleSF");

For<ISession>()
.HybridHttpOrThreadLocalScoped()
.Add(o => o.GetInstance<ISessionFactory>("OracleSF").OpenSession())
.Named("OracleSession");

For<ISessionFactory>()
.Singleton()
.Add(new NHibernateSessionFactory(<ms sql connection string>, "sql.nhibernate.config").SessionFactory)
.Named("MsSqlSF");

For<ISession>()
.HybridHttpOrThreadLocalScoped()
.Add(o => o.GetInstance<ISessionFactory>("MsSqlSF").OpenSession())
.Named("MsSqlSession");
}
}

您可以在其中使用命名实例。我的服务层使用 StructureMap 注册表类,您可以在其中定义构造函数:

this.For<IOrdersService>()
.HybridHttpOrThreadLocalScoped()
.Use<OrdersService>()
.Ctor<ISession>("sessionMDII").Is(x => x.TheInstanceNamed("OracleSession"))
.Ctor<ISession>("sessionSpedizioni").Is(x => x.TheInstanceNamed("MsSqlSession"));

对于您的服务实现:

public class OrdersService : IOrdersService
{
private readonly ISession SessionMDII;
private readonly ISession SessionSpedizioni;

public OrdersService(ISession sessionMDII, ISession sessionSpedizioni)
{
this.SessionMDII = sessionMDII;
this.SessionSpedizioni = sessionSpedizioni;
}

...
}

关于c# - 如何在 MySQL 服务器和 Microsoft SQL Server 2008 中使用 NHibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com