2016-09-30 151 views
0

我有一個web應用程序,它已經可以與SQL Server Compact一起使用。現在,我正在添加一個SQL Server模型。如果我只是簡單的通過SQL服務器連接字符串DbContext情況下,我得到以下錯誤:同時使用SQL Server Compact和SQL Server與實體框架

System.ArgumentException: Additional information: Keyword not supported: 'application name'.

我可以修復SQL服務器(但打破SQL Server精簡),如果我改變在web.config中

<entityFramework> 
- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> 
    <parameters> 
-  <parameter value="System.Data.SqlServerCe.4.0" /> 
    </parameters> 
    </defaultConnectionFactory> 
    <providers> 
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> 
    </providers> 
</entityFramework> 

要:

<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="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> 
    </providers> 
</entityFramework> 

我該如何保持雙方工作?我想我需要在web.config中添加一些<context>標記,但我沒有把它做對。

回答

0

解決!我離開web.config,因爲它最初與默認的SQL Server Compact連接工廠。唯一的區別是,我添加了SQL服務器供應商,像這樣:

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="System.Data.SqlServerCe.4.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> 
+  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

然後,在我的模型上下文類,我稱之爲DbContext(DbConnection existingConnection, bool contextOwnsConnection)構造是這樣的:

public partial class MyDbContext : DbContext 
{ 
    public MyDbContext() 
     : base(GetConnection(), true) 
    { 
    } 

    private static DbConnection GetConnection() 
    { 
     var factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); 
     var connection = factory.CreateConnection(); 
     connection.ConnectionString = "Application Name=EntityFramework;Password=foo;Persist Security Info=True;User ID=bar;Initial Catalog=MyDb;Data Source=foo.database.windows.net"; 
     return connection; 
    } 

    // ... 
} 

現在我可以使用多個提供者。

相關問題