从开发环境切换到暂存环境时出现错误 - ASP.Net Core MVC
Errors Occur when switching from Development to Staging Environment - ASP.Net Core MVC
我一直在用 ASP.Net Core MVC 使用 C# 和 Entity Framework Core 开发一个网络应用程序来与 SQL 数据库 运行 交互 [=45] =] 服务器 2017.
该应用程序现在处于 暂存阶段,但我 运行 遇到了问题。在应用程序属性中,我将 ASPNETCORE_ENVIRONMENT
变量从 Development
更改为 Staging
。现在它已更改为 Staging
,应用程序会抛出许多错误。但是,如果我将它切换回 Development
,它会正常运行。
当 运行 带有 Staging
变量的应用程序时,我得到以下错误:
我使用 Entity Framework 核心数据库优先方法自动生成数据库实体及其字段(例如 SuspicioiusTypeID
、BayID
等)。我发现的与错误相关的 solution 没有反映我的问题。我的错误只发生在我处于 Development
以外的环境中时。
导致此错误的原因是什么,我该如何解决?
Startup
、ConfigureServices
和 Configure
:
public class Startup
{
public static string ConnectionString { get; set; }
public static NICSContext Context { get; set; }
public static string version;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
ConnectionString = Configuration.GetConnectionString("DefaultConnection");
version = Configuration["Version"];
DbContextOptionsBuilder <NICSContext> optionBuilder = new DbContextOptionsBuilder<NICSContext>();
optionBuilder.UseSqlServer(ConnectionString);
Context = new NICSContext(optionBuilder.Options);
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<NICSContext>(options =>
options.UseSqlServer(ConnectionString));
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => {
options.LoginPath = "/Login";
});
// Repository Interfaces
// REMOVED FOR CLARITY AND PROPRIETARY PURPOSES
// Service Interfaces
// REMOVED FOR CLARITY AND PROPRIETARY PURPOSES
services.AddMvc(options => {
// Default policy - Authorize
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
services.AddDistributedMemoryCache();
services.AddSession();
// Singletons
services.AddSingleton(new MapperService().Mapper);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
if (env.IsStaging() || env.IsProduction())
{
app.UseExceptionHandler("/Error");
}
app.UseFileServer();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
}
我不确定这是否有影响,在我的项目中,我已经以这种方式将 dbcontext 添加到管道中。想不想试一试?
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().
AddDbContext<NICSContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
我发现了错误。我在 secret.json
中设置了我的默认连接指向我们的中央数据库,但是 appsettings.json
默认连接指向本地数据库(几个月没有更新)。一旦我将 appsettings.json
中的默认连接设置为指向我们的中央数据库,它就解决了问题。
解释:
当 ASPNETCORE_ENVIRONMENT
变量设置为 Development
时,ASP.Net 核心在查看用户机密文件(即 secrets.json
)之前查找连接字符串appsettings.json
文件。
但是,当 ASPNETCORE_ENVIRONMENT
设置为其他内容时,ASP.Net Core 不再在用户机密文件中查找连接字符串,而是在 appsettings.json
文件中查找。
我一直在用 ASP.Net Core MVC 使用 C# 和 Entity Framework Core 开发一个网络应用程序来与 SQL 数据库 运行 交互 [=45] =] 服务器 2017.
该应用程序现在处于 暂存阶段,但我 运行 遇到了问题。在应用程序属性中,我将 ASPNETCORE_ENVIRONMENT
变量从 Development
更改为 Staging
。现在它已更改为 Staging
,应用程序会抛出许多错误。但是,如果我将它切换回 Development
,它会正常运行。
当 运行 带有 Staging
变量的应用程序时,我得到以下错误:
我使用 Entity Framework 核心数据库优先方法自动生成数据库实体及其字段(例如 SuspicioiusTypeID
、BayID
等)。我发现的与错误相关的 solution 没有反映我的问题。我的错误只发生在我处于 Development
以外的环境中时。
导致此错误的原因是什么,我该如何解决?
Startup
、ConfigureServices
和 Configure
:
public class Startup
{
public static string ConnectionString { get; set; }
public static NICSContext Context { get; set; }
public static string version;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
ConnectionString = Configuration.GetConnectionString("DefaultConnection");
version = Configuration["Version"];
DbContextOptionsBuilder <NICSContext> optionBuilder = new DbContextOptionsBuilder<NICSContext>();
optionBuilder.UseSqlServer(ConnectionString);
Context = new NICSContext(optionBuilder.Options);
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<NICSContext>(options =>
options.UseSqlServer(ConnectionString));
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => {
options.LoginPath = "/Login";
});
// Repository Interfaces
// REMOVED FOR CLARITY AND PROPRIETARY PURPOSES
// Service Interfaces
// REMOVED FOR CLARITY AND PROPRIETARY PURPOSES
services.AddMvc(options => {
// Default policy - Authorize
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
services.AddDistributedMemoryCache();
services.AddSession();
// Singletons
services.AddSingleton(new MapperService().Mapper);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
if (env.IsStaging() || env.IsProduction())
{
app.UseExceptionHandler("/Error");
}
app.UseFileServer();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
}
我不确定这是否有影响,在我的项目中,我已经以这种方式将 dbcontext 添加到管道中。想不想试一试?
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().
AddDbContext<NICSContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
我发现了错误。我在 secret.json
中设置了我的默认连接指向我们的中央数据库,但是 appsettings.json
默认连接指向本地数据库(几个月没有更新)。一旦我将 appsettings.json
中的默认连接设置为指向我们的中央数据库,它就解决了问题。
解释:
当 ASPNETCORE_ENVIRONMENT
变量设置为 Development
时,ASP.Net 核心在查看用户机密文件(即 secrets.json
)之前查找连接字符串appsettings.json
文件。
但是,当 ASPNETCORE_ENVIRONMENT
设置为其他内容时,ASP.Net Core 不再在用户机密文件中查找连接字符串,而是在 appsettings.json
文件中查找。