2016-06-15 15 views
2

C#アプリケーションでEntity Framework Code-Firstを使用してPostgreSQLサーバにデータベースを作成しようとしています。私はEFのためのNpgsqlをダウンロードし、インターネット上で提示された様々な方法で私のApp.configファイルを変更しようとしましたが、何も動作しません。EntityFramework 6(C#)を使用してPostgreSQLデータベースに接続

これは私のApp.configファイルである:私はそのSqlServer作品を追加したいと思い

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /> 
    </providers> 
    </entityFramework> 
    <connectionStrings> 
    <add name="SqlServer" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=true" providerName="System.Data.SqlClient" /> 
    <add name="PostgreSql" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=true;User Id=postgre;Password=password;" providerName="Npgsql" /> 
    </connectionStrings> 
</configuration> 

。私は助けに感謝します。

+0

アプリケーションに両方のプロバイダが必要な場合は、どちらを使用するかをEFに伝える必要があります。それ以外の場合は、SqlServerエントリを削除し、デフォルトの接続ファクトリをNpgsqlが提供するものに変更します。 – Spivonious

+0

Contextクラスのコンストラクタでbase( "PostgreSql")を使用しています。どのようにデフォルトの外観にする必要がありますか? – Niko

+0

それで十分でしょう。コンテキストを使用する際にエラーが発生していますか? – Spivonious

答えて

3

私は別の構成とオプションを試していて、最終的に解決策を見つけました。

<connectionStrings> 
    <add name="SqlServer" connectionString="Data Source=localhost;Initial Catalog=SchoolDB;Integrated Security=true" providerName="System.Data.SqlClient" /> 
    <add name="PostgreSql" connectionString="Server=localhost;Port=5432;User Id=postgres;Password=1234;Database=SchoolDB;" providerName="Npgsql" /> 
    </connectionStrings> 

とプロバイダ:

接続文字列セクションは次のようにする必要があります

<providers> 
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /> 
</providers> 

また、セクションの最後に、私はそれが作成する以下の

<system.data> 
    <DbProviderFactories> 
     <remove invariant="Npgsql" /> 
     <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" support="FF" /> 
    </DbProviderFactories> 
    </system.data> 

を追加しましたDbContextベースクラスの呼び出し方法に応じて、PostgreSQLのSQL Server上の新しいデータベース。

+0

これは素晴らしいことです。私はこれで苦労していて、あなたのコードはこの問題を解決しました。共有のおかげで、私もweb.config

+0

あなたが役に立った – Niko

関連する問題