C# EntityFramework错误26,带有LocalDb和IndentityDbContext

C# EntityFramework错误26,带有LocalDb和IndentityDbContext,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我正在为MVC应用程序创建DataAccess C#类库。在类库中,我使用EntityFramework和ASP.NET标识 当我试图在类库上运行更新数据库或添加迁移时,我得到一个错误 错误26:与SQL Server建立连接时发生网络相关或特定于实例的错误。找不到或无法访问服务器。 从IdentityUser派生的我的UserAccount类如下: public class UserAccount : IdentityUser { public string First

我正在为MVC应用程序创建DataAccess C#类库。在类库中,我使用EntityFramework和ASP.NET标识

当我试图在类库上运行更新数据库添加迁移时,我得到一个错误

错误26:与SQL Server建立连接时发生网络相关或特定于实例的错误。找不到或无法访问服务器。

从IdentityUser派生的我的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类用户帐户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共虚拟地址{get;set;}
公共虚拟付款明细付款明细{get;set;}
公共虚拟媒体资源配置文件图片{get;set;}
公共虚拟ICollection会话{get;set;}
}
my context类的实现使用以下构造函数:

public class LouderContext : IdentityDbContext<UserAccount>
    {

        public LouderContext("DefaultConnection") { }
公共类LouderContext:IdentityDbContext
{
公共LouderContext(“DefaultConnection”){}
my 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>

我尝试过的事情:

  • 将连接工厂的参数从
    v11.0
  • 修复我的visual studio安装
  • 手动将SQL Server对象资源管理器中的连接字符串添加到App.config中的connectionstring对象中

  • 我可以从SSMS访问(localdb)\MSSQLSLocalDB而没有问题。我也可以通过搭建的MVC项目访问localdb,但每当我试图通过包管理器控制台在类库上运行任何命令时,我的问题就会持续存在。问题似乎出在哪里?是否有明显的跳出现象?

    请将上下文构造函数保留为空(即不调用base()),或者确保app.config中有一个connectionstring条目,用于
    DefaultConnection

    EF使用启动项目作为连接字符串的源,我认为这可能是您的问题,因为您的启动项目是MVC项目,请尝试将启动项目更改为类库,然后添加迁移和更新数据库。

    编辑:
    您有两个名称相同的连接字符串DefaultConnection一个位于app.config,另一个位于web.config,请更改该字符串,并在DBContext构造函数中添加属于app.config的字符串,例如:
    public LouderContext(“AppConfigConnection”){}

    另一个解决方案:

    Get-Help enable-migrations -detailed
    
    您可以在-StartupProjectName选项中找到此文档:

    -StartUpProjectName
    

    这意味着,如果使用此选项指定项目名称,迁移将在指定项目的配置文件中查找连接字符串。(配置文件可以是web.config或app.config)。

    您没有“DefaultConnection”您正在将其传递给配置文件中
    LouderContext
    的基类构造函数。谢谢,但我删除了它,问题仍然存在。然后将“DefaultConnection”添加到配置文件:)。
    我添加了基类(“DefaultConnection”)返回上下文并添加了连接字符串,但仍然出现错误26。我还尝试将连接更改为实际的connectionstring“Data Source=(localdb)\MSSQLLocalDB;初始目录=master;集成安全=True;连接超时=30;加密=False;信任服务器证书=False;应用内容=ReadWrite;多子网故障转移=False“但是它仍然有问题。你能用连接字符串发布你更新的app.config吗?这应该是一个注释:)谢谢,但修改后,问题仍然存在。旅游团说使用注释来澄清或询问更多信息。新用户甚至不能添加注释。请给我指一些这样的资源,我很乐意Compliance.Nice Ahmed!没有看到类库注释。谢谢!这似乎已经成功了。是否有任何方法可以让它在不指定显式连接字符串的情况下工作?您有两个名称相同的连接字符串DefaultConnection一个在app.config,一个在web.config,将其更改并在DBContext构造函数中添加一个属于app.config的,例如:public LouderContext(“AppConfigConnection”){},我将用另一个解决方案编辑我的答案。
    Specifies the configuration file to use for named connection strings. If
    omitted, the specified project's configuration file is used.