【发布时间】: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