3

我試圖獲得一個非常簡單的EF代碼第一個示例運行,但遇到了上述問題。我按照這裏的建議(How to configure ProviderManifestToken for EF Code First),但無濟於事。我最終設法通過將實際的連接字符串傳遞給DBContext而不是我定義的連接字符串名稱來獲得EF來創建我的數據庫和表。EF代碼優先 - 提供程序沒有返回ProviderManifestToken字符串

這裏是我的連接字符串最初定義中的app.config

<connectionStrings> 
    <add name="ProductContext" 
     connectionString="integrated security=SSPI; 
          data source=MYMACHINE; 
          persist security info=False; 
          initial catalog=Product" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 

「ProductContext」這個名字相匹配的類ProductContext和數據庫產品不存在。

繼上一個線程的建議之後,我將連接字符串名稱傳遞給ProductContext和基本DBContext cstor。這也沒有用。

最後,我傳遞了上面的連接字符串,所有工作,數據庫創建和表。

我正在使用SQL Server 2008和EF 4.1。任何想法,我做錯了什麼?

感謝,

更新

應用程序是一個WPF應用程序,而不是一個Web應用程序。我得到同樣的異常後,我從的app.config刪除的ConnectionString:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

+0

您正在運行的項目的* app.config *中的連接字符串還是位於庫項目中? – Slauma

+0

它在主項目的app.config而不是庫中。如何解決這個問題? – Lance

+0

試圖添加一個app.config到庫項目,並嘗試共享主項目app.config,但都沒有辦法。 – Lance

回答

3

與回答這個問題的問題是,罪魁禍首可能是不止一個問題。

這麼說,我看到一對夫婦的事情來檢查:

  1. 您的數據源名稱看起來是無效。您需要聲明主機名和實例名稱。示例:。\ SQLEXPRESS(請注意。dot)或MyServerHostName \ MySqlInstanceName

    使用SQL Server Management Studio驗證您是否有有效的數據源,然後在那裏嘗試它。

  2. 權限。您正在使用集成安全性,因此進行數據庫連接的應用程序的安全上下文必須具有適當的權限。需要檢查的事項:

    使用SQL Server Mgmt Studio連接到您的數據庫。在左側窗格(對象資源管理器) - >數據庫 - >選擇你的數據庫 - >安全 - >用戶中選擇你的數據庫。確保您可以驗證用於運行應用程序的帳戶是否已被明確授予權限,或者可以通過成爲該組的成員來繼承它們。

When I had this error, my problem proved to be that I had mixed Integrated Security=SSPI with a "Username =" and "Password =" ...not realizing that I needed Integrated Security=false; my user and password parameters were being ignored.

Not sure about this one: I did read some people suggest (at least for troubleshooting) to set Persist Security Info=true.

3

我有類似的問題上EF6 VS 2013,問題得到有效解決,當我選擇在實體框架「保存密碼」 - 逆向工程的Code First - 連接屬性對話框。希望這可以幫助某人。

+0

這是解決方案。 – mlqmarkos12

+0

weeeeeird,但是的確如此 – user1916015

相關問題