1

我正在使用以下代码来创建Session Factory带有 NHibernate 的。我想用相对路径来指定这个路径,这样当我和我的队友合并我们的分支时,我们就不必一直修复连接字符串。我怎样才能做到这一点?

private static void InitializeSessionFactory(bool deleteSchemaOnClosing = false, bool regenerateSchemaOnOpening = false)
{
_sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
                  .ConnectionString(
                      @"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\MyProject\MyProjectCore\Fluent NHibernate\CoreDatabase.mdf"";Integrated Security=True;User Instance=True")
                  .ShowSql()
    )
    .Mappings(m =>
              m.FluentMappings
                  .AddFromAssemblyOf<User>())
    .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                    .Create(deleteSchemaOnClosing,regenerateSchemaOnOpening))
    .BuildSessionFactory();
}

我的app.config文件如下所示:

<?xml version="1.0"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="CoreDatabase.Properties.Settings.CoreDatabaseConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CoreDatabase.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
        <add name="Fluent_NHibernate.Properties.Settings.CoreDatabaseConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CoreDatabase.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>

谢谢!

4

2 回答 2

3

看看 SqlConnectionStringBuilder,你可以这样做:

var rawStr = Settings.CoreDatabaseConnectionString.ConnectionString;
// retrieve the original connection string from config file

var conBuilder = new SqlConnectionStringBuilder(rawStr);
conBuilder.AttachDBFilename = Path.GetFullPath(
    Path.Combine(Environment.CurrentDirectory, "CoreDatabase.mdf"));
// if you're doing this in a web environment, swap Environment.CurrentDirectory 
// for HttpRuntime.AppDomainAppPath

_sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
                  .ConnectionString(conBuilder.ToString())
                  .ShowSql()
    )
// rest of your configuration...
于 2012-10-17T09:35:57.357 回答
0

作为一个示例,您可以遵循类似这样的内容.. 这将是一种将值保存到 .Config 文件的方法,但只是为了给您一个示例,说明如何在 .Config 文件中使用参数,请看一下这个。

如果您确实需要一个示例,我还将以更简单的方式附加到我的答案中

//Add config like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>

  </connectionStrings>
</configuration>


 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(
                                                          "MyConnectionString",
                                                          String.Format("DataSource={0};InitialCatalog={1};IntegratedSecurity={2}",
                                                                         "testing", "testing2", "Testing6")));
            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("connectionStrings");
            MessageBox.Show(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)

看看如何从配置文件中读取,并尝试链接上的示例代码并逐步执行代码,这将是一次很棒的学习体验...... 如何从 .Config 文件中读取应用程序设置

于 2012-10-16T23:28:13.843 回答