我在Windows 8中使用VisualStudio2019,我有一个测试数据库和网站,我在过去的一个月里一直在做这个工作。我用Azure做主人。我在实体框架中使用ASP.Net MVC Core3.1。我已经准备好把所有的东西都移植到现场。但是,我不知道如何让Update-Database针对活动数据库。每次我运行它时,它都会尝试更新测试数据库。我想我只需要更改在Startup.ConfigureServices(..)中引用的连接字符串。
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<IFileProvider>(
new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));
services.AddControllersWithViews();
services.AddDbContext<MapItGO_DbContext>(opts => {
opts.UseSqlServer(
Configuration["ConnectionStrings:MapItGODBConnection"]); //"ConnectionStrings:MapItGODBConnection/MapItGOTestDBConnection"
});我在ConnectionStrings中定义了多个appsettings.json:
"ConnectionStrings": {
"MapItGoDBConnection": "Server=tcp:mapitgo.database.windows.net,1433;Initial Catalog=mapitgodb;Persist Security Info=False;User ID=***;Password=***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"MapItGOTestDBConnection": "Server=tcp:mapitgo.database.windows.net,1433;Initial Catalog=mapitgotestdb;Persist Security Info=False;User ID=***;Password=***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"MapItGo_Web_AppContextConnection": "Server=(localdb)\\mssqllocaldb;Database=MapItGo Web App;Trusted_Connection=True;MultipleActiveResultSets=true"
}我正在失去我现在剩下的头发。本来应该是快速而轻松的生活已经消耗了一天中的大部分时间。我愿意接受任何建议。
谢谢!
编辑:
PM> update-database -verbose
Using project 'MapItGo Web App'.
Using startup project 'MapItGo Web App'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile "C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\bin\Debug\netcoreapp3.1\MapItGo Web App.deps.json" --additionalprobingpath C:\Users\gregoryshill\.nuget\packages --runtimeconfig "C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\bin\Debug\netcoreapp3.1\MapItGo Web App.runtimeconfig.json" C:\Users\gregoryshill\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.14\tools\netcoreapp2.0\any\ef.dll database update --verbose --no-color --prefix-output --assembly "C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\bin\Debug\netcoreapp3.1\MapItGo Web App.dll" --startup-assembly "C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\bin\Debug\netcoreapp3.1\MapItGo Web App.dll" --project-dir "C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\\" --language C# --working-dir "C:\Users\gregoryshill\source\repos\MapItGo Web App" --root-namespace MapItGo_Web_App
Using assembly 'MapItGo Web App'.
Using startup assembly 'MapItGo Web App'.
Using application base 'C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\bin\Debug\netcoreapp3.1'.
Using working directory 'C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App'.
Using root namespace 'MapItGo_Web_App'.
Using project directory 'C:\Users\gregoryshill\source\repos\MapItGo Web App\MapItGo Web App\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'MapItGO_DbContext'.
Finding DbContext classes in the project...
Using context 'MapItGO_DbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'MapItGo Web App'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'MapItGo Web App'...
No design-time services were found.
Done.而且,不,我还没有覆盖OnConfiguring。什么属于那里?
更新:
这是在appsettings.Development.json中的:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}就这样。
更新(2021-06-28):我明白了.见下面我最后的评论。它被埋在secrets.json文件中。
发布于 2021-06-29 07:45:28
要做到这一点,应用程序必须从某个地方读取无效的连接字符串。您需要从测试连接字符串中擦除应用程序的每个部分。这包括:
appsettings.json和appsettings.$env.jsonConnectionStrings__$MyConnectionStringdotnet user-secrets list来检查它。或者,检查IConfiguration.GetDebugView()并查看每个配置的来源。
找到它之后,将连接字符串更改为真正的连接字符串。
https://stackoverflow.com/questions/68137050
复制相似问题