2015-12-21 79 views
0

我最初是我的應用程序的數據模型的代碼第一次使用EF6和的LocalDB所有已工作的罰款。代碼首先EF在本地數據庫 - 移動到企業的SQL Server

我現在需要到數據庫移動到我們企業的SQL Server環境,但我沒有相應的權限,以便能夠使用代碼第一次......所有的腳本來創建/修改都將產生,評估,審批,由數據庫管理員執行..

我的問題是,我有作爲的人我已經嘗試都失敗了與我的數據庫連接字符串的問題。如前所述,在使用LocalDB之前,使用了默認的連接字符串。

的DbContext構造

public HotelRequestDBContext() : base("HotelRequestsDB") 
{ 
    //Database.SetInitializer<HotelRequestDBContext>(new HotelRequestDBContextInitialiser()); 

    if (Properties.Settings.Default.EFTrace) 
    { 
     this.Database.Log = msg => System.Diagnostics.Trace.WriteLine(msg); 
    } 

} 

連接字符串嘗試1

<add name="HotelRequestsDB" connectionString="Data Source=server\instance;initial catalog=HoteRequests;persist security info=True;user id=username;password=password;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.EntityClient" /> 

Exception thrown: 'System.ArgumentException' in EntityFramework.dll

Additional information: Keyword not supported: 'data source'.

If there is a handler for this exception, the program may be safely continued.

連接字符串嘗試2

<add name="HoteRequestsDB" connectionString="metadata=res://*/HoteRequests.csdl|res://*/HoteRequests.ssdl|res://*/HoteRequests.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server\instance;initial catalog=HoteRequests;persist security info=True;user id=user;password=password;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 

異常彈出窗口

Exception thrown: 'System.Data.DataException' in EntityFramework.dll

Additional information: An exception occurred while initializing the database. See the InnerException for details.

If there is a handler for this exception, the program may be safely continued.

調試輸出窗口

Cannot attach the file 'C:\Projects\HotelRequests\HotelRequests\App_Data\HotelRequestsDB.mdf' as database 'HotelRequestsDB'.

+0

什麼是您的錯誤信息? – strickt01

+0

更新了問題 –

+0

只是要清楚,數據庫已經在sql服務器上了嗎?如你所說,你不能使用代碼首先自己創建它們 –

回答

1

您的ConnectionString年底providerName="System.Data.EntityClient"是什麼,通常導致此。在啓動項目的配置文件中的某處,您應該看到一些帶有<entityFramework>標籤的xml。它是自動生成的。

連接到您需要確保<provider>一個SQL服務器如下:

<entityFramework> 
<providers> 
    <provider invariantName="System.Data.EntityClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
</providers> 

(你應該將名稱更改爲System.Data.SqlClient過,無論是在你的ConnectionString和的providerName到是正確的。)

+1

創建好了,除了一個事實,即我有「初始目錄」的名稱不正確,更改連接字符串條目末尾的「提供者」解決了問題...謝謝 –

+0

嗯,你忘了'我'必須愛錯別字;) –