从开发环境切换到暂存环境时发生错误-ASP.Net Core MVC

时间:2018-09-20 13:49:57

标签: sql-server visual-studio asp.net-core-mvc entity-framework-core staging

我一直在使用ASP.Net Core MVC使用C#和Entity Framework Core开发一个Web应用程序,以与在SQL Server 2017上运行的SQL数据库进行交互。

该应用程序现在处于登台中,但是我遇到了问题。在应用程序属性中,将ASPNETCORE_ENVIRONMENT变量从Development更改为Staging。现在将其更改为Staging,该应用程序将引发许多错误。但是,如果我将其切换回Development,它将正常运行。

使用Staging变量运行应用程序时出现以下错误:

Error when moving into the Staging Environment

我使用实体框架核心数据库优先方法自动生成数据库实体及其字段(例如SuspicioiusTypeIDBayID等)。我发现与该错误有关的solution不能解决我的问题。仅当我在Development以外的环境中时,才会发生我的错误。

什么原因导致此错误,该如何解决?

StartupConfigureServicesConfigure

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?}");
        });
    }

2 个答案:

答案 0 :(得分:1)

我发现了错误。我已经在secret.json中设置了指向我们的集中式数据库的默认连接,但是appsettings.json的默认连接则指向了本地数据库(几个月以来没有更新)。一旦我在appsettings.json中设置了默认连接以指向我们的集中式数据库,就解决了该问题。

说明:

ASPNETCORE_ENVIRONMENT变量设置为Development时,ASP.Net Core会在查看{{1}之前在User Secrets文件(即secrets.json)中查找连接字符串。 }文件。

但是,当appsettings.json设置为其他设置时,ASP.Net Core不再在用户密钥文件中查找连接字符串,而是在ASPNETCORE_ENVIRONMENT文件中查找。

答案 1 :(得分:0)

我不确定是否会有所作为,在我的项目中,我已经以这种方式将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")));
    }