2017-01-22 9 views
2

FluentNHibernateを使用すると、web.configから接続文字列を読み取ることができません。私はASP.NET Core Webアプリケーション(.NET Framework)、Fluent NHibernate 2.0.3およびNHibernate 4.0.0.4000を使用しています。FluentNHibernateがweb.configからconnectionStringを読み取れません

コメント付きの方法ではデータベースにアクセスできますが、コメントされていないものは機能しません。

 return Fluently.Configure() 
      //.Database(MySQLConfiguration.Standard.ConnectionString("Server=localhost;Port=3307; Uid=root; Pwd=usbw;Database=hellonhibernate")) 
      .Database(MySQLConfiguration.Standard.ConnectionString(c => c.FromConnectionStringWithKey("TRP123456"))) 
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PersonMap>()) 
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionMap>()) 
      .ExposeConfiguration(CreateSchema) 
      .BuildSessionFactory(); 

web.configファイルには、これは私が得たエラーです

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
    <handlers> 
     <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> 
    </handlers> 
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> 
    </system.webServer> 
    <connectionStrings> 
     <add name="TRP123456" connectionString="server=localhost;port=3307;uid=root;password=admin;database=hellonhibernate;" providerName="MySql.Data.MySqlClient"/> 
    </connectionStrings> 
</configuration> 

です。何が間違っているかを確認してください。ありがとうございました。

enter image description here

+0

'Database(MySQLConfiguration.Standard.ConnectionString(" TRP123456 "))。データベース(MySQLConfiguration.Standard.ConnectionString(" TRP123456 ")) ))) ' –

+0

こんにちは、お世話になりました。しかし、それは動作しないようです。私は次のコードを変更しましたが、まだNullエラーが発生します。データベース(MySQLConfiguration.Standard.ConnectionString(ConfigurationManager.ConnectionStrings ["TR P123456"])。ConnectionString)) – Ben

+0

接続文字列を正しい 'config'ファイルに入れてください。 1つのアプリケーションしかありませんか? –

答えて

1

最初のチェックをたどるよう

enter image description here

プロジェクトの構造があり、私はそれはあなたの問題を解決するかもしれないと思うこの質問Access WebConfig in DotNet Core別の解決策は、appsettings.json使用することです

代わりにファイル。

これは、プロジェクトにjsonファイルを追加することで可能です。 project.jsonファイルと同じフォルダ。

あなただけの、次のようにアプリの設定にファイルをマップあなたのstartup.csで

public static IConfigurationRoot Configuration = null; 
public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // in my case the file name is appsettings.json 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
      .AddEnvironmentVariables(); 

     Configuration = builder.Build(); // returns IConfigurationRoot that can be used to access your settings later 
    } 

今、私はそれが行くための最善の方法ではないかもしれない、私はそこに作成された静的変数については確認していませんそれについて。しかし、それは私に必要なものを与えました。

あなたは、そのようなあなたたconnectionStringを得るためにそれを使用することができます:

Startup.Configuration["AppSettings:ConnectionString"] 

あなたappsettings.jsonファイル内のConnectionStringキーを持っていることを考えます。

{ 
"AppSettings": 
    { 
    "ConnectionString": "" //type your connection string in here 
    } 
} 

にも優れていることは、このようのAppSettingsクラスを追加することです:

public class AppSettings 
{ 
    public string ConnectionString { get; set; } 
} 

今、あなたは(あなたのミドルウェアにそれを追加)サービスとして、それをマップする必要があります。

public void ConfigureServices(IServiceCollection services) 
    { 
     //other services 
     services.Configure<AppSettings> Configuration.GetSection("AppSettings")); 
     //other services 
    } 

つまり、あなたのコントローラにそれを注入することができます

public class HomeController : Controller 
{ 
    public _appsettings; 
    public HomeController(IOptions<AppSettings> appSettings) 
    { 
     _appsettings = appSettings; 
    } 
} 

最後に、あなたは今、あなたのコントローラで_appsettings.ConnectionStringを呼び出すことによって、あなたの接続文字列を取得することができます。

関連する問題