2016-07-19 15 views
0

私はMyFilterと組み合わせてEFを使用してASP.net MVCプロジェクトを作成しています。今はこれだけでうまく動作しますが、SQLでEFを使用する別のクラスライブラリを参照したいこともあります。同じプロジェクト内でmysqlとsqlでEF DbContextを使用

ライブラリを参照すると、各DbContextにどのプロバイダを使用するかについてEFが混乱するようです。私はこのDbContextは、MySQLプロバイダによって処理されるべきであるEFを伝えるために、次の属性持っているわたしのMySQL DbContextで

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 

今SQL DbContext上を、私は何の属性を持っていません。私はそこに1つを置くべきでしょうか?私はこの問題についていくつか検索しましたが、どこでも正しい答えを見つけることができませんでした。

The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered. An instance of 'MySqlEFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.

これは、SQLコンテキストは、MySQL 1の前に使用されているが、私はこの上の修正を見つけるように見えることができないので、かなり単純です:

現在、私は次のエラーを取得します。

これを処理する上でのベストプラクティスは何でしょうか。または、同じプロジェクトで2つのDbContextを組み合わせて、これを回避する必要があるものです。

ありがとうございました!

のMySQL DbContext

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 
public class MySqlContext : DbContext 
{ 
    public MySqlContext() : base("name=MySqlContext") 
    { 
     this.Configuration.LazyLoadingEnabled = false; 
    } 
    //DbSets.... 
} 

SQL DbContext

public class SqlContext : DbContext 
{ 
    public SqlContext() : base("name=SqlContext") 
    { 
     Configuration.LazyLoadingEnabled = false; 
    } 
    //DbSets.... 
} 

のWeb.config:

<connectionStrings> 
    <add name="SqlContext" connectionString="some connectionString" providerName="System.Data.SqlClient" /> 
    <add name="MysqlContext" connectionString="some connectionString" providerName="MySql.Data.MySqlClient" /> 
    </connectionStrings> 

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <system.data> 
    <DbProviderFactories> 
     <remove invariant="MySql.Data.MySqlClient" /> 
     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 
    </DbProviderFactories> 
    </system.data> 

私が見O n DbProviderFactoriesはその唯一のMysqlです。

+0

でのConnectionStringとのAppConfigの例。 web.configを投稿できますか? –

+0

これはあなたを助けるかもしれません:http://stackoverflow.com/questions/24766068/entityframework-two-different-providers-on-the-same-config-file –

+0

私はすぐにテストする環境がありません。これをSqlContextの上に置いてみてください。 '[DbConfigurationType(typeof(System.Data.Entity.DbConfiguration))]' –

答えて

0

(エンティティフレームワーク6では)所有するDbContextの数は重要ではありません。スタートアッププロジェクトのappConfigまたはwebConfigに接続文字列を入力するだけです。

2はEfの6.01 & SQL Serverのと複数のdbcontextを使用することは完全に罰金私のSQL

<configSections> 
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> 
<connectionStrings> 
    <add name="MySqlDB" connectionString="Your My SQL ConnectionString" providerName="My Sql Provider" /> 
    <add name="SqlDB" connectionString="Your SQL ConnectionString" providerName="Sql Provider" /> 
</connectionStrings> 
<entityFramework> 
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> 
    <parameters> 
    <parameter value="System.Data.SqlServerCe.4.0" /> 
    </parameters> 
</defaultConnectionFactory> 
<providers> 
    <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> 
</providers> 
</entityFramework> 

のMySQL DbContext

public class MySqlContext : DbContext 
{ 
    public MySqlContext() : base("MySqlDB") 
    { 
     this.Configuration.LazyLoadingEnabled = false; 
    } 
//DbSets.... 
} 

SQL DbContext

public class SqlContext : DbContext 
{ 
    public SqlContext() : base("SqlDB") 
    { 
     Configuration.LazyLoadingEnabled = false; 
    } 
    //DbSets.... 
} 
+0

私の編集をご覧ください。私はweb.configに2つの接続文字列と両方のプロバイダを設定しています。私がそれを変更すると、SqlCeProvider Sqlも単独で失敗します。 SqlサーバーはSqlCompactサーバーでもありません。 –

+0

私は自分の答えを編集しました。もう一度やり直してください。 –

+0

MySql属性を取り除くことは、そのトリックでした。私はこれをもっと早く試していないとは信じられません。ありがとう! FyiをSystem.Data.SqlServerCe.4.0でコンパイルしていないため動作しません。 –

関連する問題