具有localdb v13底层连接的EF6无法打开

时间:2016-07-05 19:22:12

标签: c# sql sql-server asp.net-mvc entity-framework

我有一个简单的控制台应用程序与EF6,我的电脑有localdb v13,当我尝试连接到EF数据库时,我收到此错误

{"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>

1 个答案:

答案 0 :(得分:1)

控制台应用程序可能还有一个App.config无关紧要,通常情况下,如果您已经从Package Manager控制台安装了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>

您应该使用&#39; name&#39;将连接字符串传递给DbContext构造函数。这样的关键字:

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

我在app.config示例中使用了普通的SQL Server:

Data Source=.\;

你可以改变它:

Data Source=(localdb)\\mssqllocaldb;

或者只检查您的SQL Server实例名称(使用SSM或命令行)。