EF6用的LocalDB V13底层连接未能打开
问题描述:
我有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>
答
这是不要紧的控制台应用程序也可能有一个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或命令线)。
我是管理员在sql server本地db btw ... –
C:\ WINDOWS \ system32> sqllocaldb.exe创建 La instancia“MSSQLLocalDB”de LocalDB secreócon laversión13.0.1100.286。 –
在AppData –