2015-12-31 36 views
4

我正在爲MVC應用程序創建一個DataAccess C#類庫。在類庫裏面,我使用了EntityFramework和ASP.NET Identity。EntityFramework錯誤26與LocalDb和IndentityDbContext

當我嘗試在類庫運行更新,數據庫附加遷移,我得到一個錯誤。從IdentityUser衍生

Error 26:A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

我UserAccount類如下:

public class UserAccount : IdentityUser 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 

     public virtual Address Address { get; set; } 
     public virtual PaymentDetail PaymentDetail { get; set; } 

     public virtual MediaResource ProfilePicture { get; set; } 
     public virtual ICollection<Conversation> Conversations { get; set; } 
    } 

我的上下文類的實現使用這個構造:

public class LouderContext : IdentityDbContext<UserAccount> 
    { 

     public LouderContext("DefaultConnection") { } 

我App.config文件的內容如下:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v12.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

的事情,我曾嘗試:

  1. v11.0
  2. 更改連接工廠的參數mssqllocaldbv12.0修復我的Visual Studio安裝
  3. 手動從SQL Server對象資源管理器中添加連接字符串到App.config中的連接字符串對象

我可以從SSMS訪問(localdb)\ MSSQLLocalDB沒有問題。我也可以通過一個腳手架的MVC項目訪問LocalDb,但是當我嘗試通過包管理器控制檯在我的類庫上運行任何命令時,我的問題依然存在。什麼似乎是這個問題?有什麼明顯的跳出來嗎?

+1

您沒有在您的配置文件中傳遞給「LouderContext」的基類構造函數的「DefaultConnection」。 – dotctor

+0

謝謝,但我刪除它,問題依然存在。 – CanadaIT

+0

然後添加一個「DefaultConnection」到你的配置文件:)。 < connectionStrings>' – dotctor

回答

1

EF使用啓動項目作爲連接字符串的來源,我認爲這可能是您的問題,因爲您的啓動項目是MVC項目嘗試將啓動項目更改爲類庫,然後添加遷移和更新數據庫。

編輯: 你必須使用相同的名稱DefaultConnection一個在app.config中,一個在web.config中兩個連接字符串,更改和構造的DbContext添加一個屬於的app.config例如:public LouderContext("AppConfigConnection") { }

另一種解決方案:

Get-Help enable-migrations -detailed 

你可以找到這個文件的-StartupProjectName選項:

-StartUpProjectName 
Specifies the configuration file to use for named connection strings. If 
omitted, the specified project's configuration file is used. 

這意味着,如果你使用這個選項來指定一個項目名稱,遷移將查找在指定項目的配置文件中的連接字符串。 (配置文件可以是web.config或app.config)。

+0

不錯的艾哈邁德!沒有看到類庫評論。 – Bubba

+0

謝謝!這似乎已經成功了。有什麼方法可以讓我的工作沒有指定明確的連接字符串? – CanadaIT

+0

您有兩個具有相同名稱的連接字符串DefaultConnection,其中一個位於app.config,另一個位於web.config,更改並在DBContext構造函數中添加屬於app.config EX的一個:public LouderContext(「AppConfigConnection」){},我將用另一種解決方案編輯我的答案。 –

0

要麼將​​Context構造函數留空(即不要調用base()),要麼確保DefaultConnection的app.config中存在連接字符串條目。

+1

這應該是一個評論:) – dotctor

+0

謝謝但修改後,問題仍然存在。 – CanadaIT

+0

那麼遊覽說使用評論澄清或要求更多的信息。新用戶甚至無法添加評論。請給我指出一些資源,我會很樂意遵守。 – Bubba