2016-07-05 50 views
1

我有EF6一個簡單的控制檯應用程序,我的電腦有的LocalDB V13,當我嘗試連接到EF數據庫,我得到這個錯誤EF6用的LocalDB V13底層連接未能打開

{"Cannot open database \"NinjaDomain.DataModel.NinjaContext\" requested by the login. The login failed.\r\nLogin failed for user 'xx\\xx.yy'."} 

這是在app.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <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" /> 
    </providers> 
    </entityFramework> 
</configuration> 
+0

我是管理員在sql server本地db btw ... –

+0

C:\ WINDOWS \ system32> sqllocaldb.exe創建 La instancia「MSSQLLocalDB」de LocalDB secreócon laversión13.0.1100.286。 –

+0

在AppData –

回答

1

這是不要緊的控制檯應用程序也可能有一個app.config,一般來說,如果你已經從程序包管理器控制檯安裝EF這樣的:

install-package entityframework 

app.config將自動添加到您的項目文件中。

這是默認的連接字符串,如果沒有它在app.config(與EF v 6.1.3)

"Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MyConsoleApplication.Program+MyDbContext;Integrated Security=True;MultipleActiveResultSets=True" 

下面,我已經改變了的app.config(如果你這樣做沒有你可以把它添加到您的項目),我所提供的連接字符串

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> 
    </startup> 
    <connectionStrings> 
    <add name="DatabaseConnectionString" 
     connectionString="Integrated Security=SSPI; Initial Catalog=UsersDatabase5; Data Source=.\;" providerName="System.Data.SqlClient" /> 
    </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" /> 
    </providers> 
    </entityFramework> 
</configuration> 

你應該在連接字符串傳遞到的DbContext構造與這樣的「名」關鍵字:

using (var myDbContext = new MyDbContext("name=" + "DatabaseConnectionString")) 
{ 
.... 

我用正常的SQL Server在我的app.config例如:

Data Source=.\; 

你可以改變它:

Data Source=(localdb)\\mssqllocaldb; 

或只是檢查你的SQL Server實例名稱(使用SSM或命令線)。