語境
我正在關注Microsoft 在 ASP.NET Core 5.0 中的 MVC 教程,使用 VS Code 和終端,但我陷入了初始遷移步驟。
關于 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>
如果您想查看代碼的其他部分,我已將該專案托管在 GitHub 上。或者,你可以告訴我,我會放上來。
uj5u.com熱心網友回復:
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 風格的連接字串(這將合理地具有Server和Database關鍵字),這是可以預料的。
您還可以查看 connectionstrings.com 以獲取每個 DB 的一些連接字串示例,并適當地形成相關字串 - 最終,這兩個 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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327180.html
標籤:C# sql asp.net asp.net-mvc asp.net核心
