代码之家  ›  专栏  ›  技术社区  ›  Corey P

从开发环境切换到暂存环境时出错-ASP.Net 核心MVC

  •  0
  • Corey P  · 技术社区  · 6 年前

    我一直在用ASP.Net 核心MVC使用C#和实体框架核心与SQL Server 2017上运行的SQL数据库进行交互。

    应用程序正在运行 登台 ,但我遇到了问题。在应用程序属性中,我更改 ASPNETCORE_ENVIRONMENT 变量来自 Development Staging . 现在改成了 发展

    在使用 登台

    Error when moving into the Staging Environment

    我使用EntityFramework核心数据库优先方法自动生成数据库实体及其字段(例如。 SuspicioiusTypeID , BayID 等)。A solution 发展 .

    是什么原因导致此错误?如何修复它?

    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?}");
            });
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Corey P    6 年前

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

    ASPNETCORE_ENVIRONMENT 变量设置为 Development , ASP.Net 核心查找用户机密文件(即。 secrets.json )在查找 文件。

    ASPNETCORE\u环境 准备做别的事,ASP.Net 核心不再在用户机密文件中查找连接字符串,而是 appsettings.json

        2
  •  0
  •   Muheeb    6 年前

    我不确定这是否有什么不同,在我的项目中,我以这种方式将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")));
        }