【问题标题】:Connection string keyword 'server' is not supported—when following the ASP.NET tutorial on MS Docs不支持连接字符串关键字“服务器”——遵循 MS Docs 上的 ASP.NET 教程时
【发布时间】:2021-12-07 10:13:07
【问题描述】:

上下文

我正在关注Microsoft's tutorial on MVC in ASP.NET Core 5.0,使用 VS Code 和终端,但我卡在了the Initial Migration step

关于 SO 有许多类似的问题,但没有一个包含我能理解的答案,因为这是我第一次深入研究 ASP.NET。

问题描述

本教程要求我运行以下命令:

dotnet ef migrations add InitialCreate
dotnet ef database update

第一个运行顺利。第二个导致以下错误:

Build started...
Build succeeded.
System.ArgumentException: Connection string keyword 'server' is not supported. For a possible alternative, see https://go.microsoft.com/fwlink/?linkid=2142181.
   at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(String keyword)
   at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.set_Item(String keyword, Object value)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Connection string keyword 'server' is not supported. For a possible alternative, see https://go.microsoft.com/fwlink/?linkid=2142181.

设置

这是我的appsettings.json 文件,带有连接字符串的文件:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-9dffe5a0-829d-4c64-9129-54ea0791196d;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

这里是appsettings.Development.json,它缺少任何连接字符串:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

我尝试将 "AllowedHosts""ConnectionString" 条目添加到此文件中,因为我认为我现在应该处于开发模式,但它没有改变任何东西。

如果我理解正确,我的项目在开发中使用 SQLite,在生产中使用 SQLServer

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    services.AddDbContext<MvcMovieContext>(options =>
    {
        var connectionString = Configuration.GetConnectionString("MvcMovieContext");

        if (Environment.IsDevelopment())
            options.UseSqlite(connectionString);
        else
            options.UseSqlServer(connectionString);
    });
}

最后,这是我的.csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.0-*" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0-*" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.0-*" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.11">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="5.0.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
  </ItemGroup>

</Project>

如果您想查看代码的其他部分,I've hosted the project on GitHub。或者,你可以告诉我,我会贴出来的。

【问题讨论】:

  • 如果您不确定连接字符串需要采用哪种格式来匹配 DB 类型,那么connectionstrings.com 就是您的朋友。

标签: c# sql asp.net asp.net-mvc asp.net-core


【解决方案1】:

Sqlite 不是基于服务器的数据库;您使用的提供程序不理解您在连接字符串中使用的 Server 关键字。尝试指定Data Source=some_path

有关有效关键字的列表,请参阅https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/connection-strings

旁注:SQLServer 的提供程序将Server 理解为Data Source 的同义词

如果您删除/修复一个关键字,您会看到错误已转移到抱怨下一个关键字 (Database);本质上,如果您尝试在 SQLite 中使用 SQLServer 风格的连接字符串(这将合理地包含 ServerDatabase 关键字),这是可以预料的。

您还可以查看 connectionstrings.com,了解每个 DB 的 connstrs 的一些示例,并适当地形成相关字符串 - 最终,当涉及到您在连接字符串中使用的关键字时,这两个 DB 有非常不同的需求。

只有两个字符串可能是最简单的:

  "ConnectionStrings": {
    "MvcMovieContextSQLS": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-9dffe5a0-829d-4c64-9129-54ea0791196d;Trusted_Connection=True;MultipleActiveResultSets=true",
    "MvcMovieContextSQLite": "Data Source=c:\path\to\my.db"
  }

您的代码会适当地拉动它们:

    if (Environment.IsDevelopment())
        options.UseSqlite(Configuration.GetConnectionString("MvcMovieContextSQLite"));
    else
        options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContextSQLS"));

或者,如果您决定“dev 是 SQLite,live 是 SQLS”,您可以通过在 appsettings.Development.json 中指定相同的组 (ConnectionStrings) 和属性 (MvcMovieContext) 来让您的开发配置覆盖您的主配置:

  "ConnectionStrings": {
    "MvcMovieContext": "Data Source=c:\path\to\my.db"
  }

出于各种原因,我倾向于不走这条路,但如果您记得appsettings.Development.json 替换了appsettings.json 中的任何匹配元素,那将是完全有效的。

【讨论】:

  • 谢谢。虽然,你的意思是DataSource(没有空格)?我将其更改为 appsettings.Development.json 中的那个,并且该特定错误消失了。现在,有人告诉我,“不支持连接字符串关键字‘数据库’。”我应该用什么替换 that
  • DatasourceData Source 的别名/同义词
  • 类似的原因; Data Source 设置指定了 sqlite db 文件(或者您可能正在使用 in-mem 并且没有文件),您不需要 database 关键字(并且提供程序不支持它) - 它适用于像 SQLServer 这样的系统,其中一台服务器托管多个数据库
  • (解决方法本质上是错误信息中的:go.microsoft.com/fwlink/?linkid=2142181
  • (旁注 2:连接字符串中的许多东西是同义词。对于 SQLServer connstr Data Source, Server, Addr, Address, Network Address 都意味着相同的东西:要连接的服务器。即使存在交叉,就像两个提供程序一样支持Data Source,您最好只提供不同的字符串,而不是试图找到一种方法让一个字符串适用于两个不同的数据库提供程序
猜你喜欢
  • 2021-11-09
  • 2018-07-10
  • 1970-01-01
  • 2011-10-02
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
相关资源
最近更新 更多