【问题标题】:Connection string not working in ASP.NET Core连接字符串在 ASP.NET Core 中不起作用
【发布时间】:2018-03-04 18:32:20
【问题描述】:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=ABC; Trusted_Connection=True; MultipleActiveResultSets=true"}

我正在使用 ASP.NET Core MVC。我的 appsettings.json 文件中有这个连接字符串,但它似乎不起作用。从 cmd 运行“dotnet ef 数据库更新”时,我收到此错误 keyword not supported: 'server.'。它有什么问题?

【问题讨论】:

  • 是否可以假设,您正在使用 EntityFramework 进行此连接?
  • 那不是 EF 连接字符串
  • @Nkosi,我知道,这就是为什么我想知道 OP 是否将它与 EF 一起使用,这将解释错误。
  • 是的,我正在使用 EF

标签: asp.net-mvc asp.net-core


【解决方案1】:

抱歉!在 Startup.cs 的 ConfigureServices 方法中,我使用的是 SQLite 数据库提供程序

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")))

我将其更改为以下内容,并且它与我的连接字符串一起使用。

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))

【讨论】:

    【解决方案2】:

    连接字符串应该以Data Source=开头。

    在 Visual Studio 中,如果您打开 SQL Server 对象资源管理器并单击要连接的数据库。连接字符串将显示在“属性”窗口中。 localDb 的连接字符串应该是这样的

    Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DbName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=Fals
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2017-08-18
      • 1970-01-01
      • 2011-04-03
      • 2021-01-28
      • 1970-01-01
      相关资源
      最近更新 更多