在Heroku上部署之前无法为.Net Core运行迁移

时间:2019-06-14 14:19:20

标签: c# postgresql entity-framework heroku .net-core

在将postgresql db附加到应用程序后,我发现需要运行迁移,因此我使用docker将一个dotnet核心api托管在heroku上进行部署,因此我将这部分添加到了.csproj文件中。

<Target Name="PrePublishTarget" AfterTargets="Publish">
  <Exec Command="dotnet ef database update" />
</Target>

这是我的创业公司

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    readonly string mySpecificOrigin = "VotingBlockOrigin";

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IElectionRepository, ElectionRepository>();
        services.AddSingleton<IConfiguration>(Configuration);
        var appSettingsSection = Configuration.GetSection("AppSettings");
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddAutoMapper();
        services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
        services.AddDbContext<VotingDBContext>(options =>{

            var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            string connStr;
            if (env == "Development") {
                connStr = Configuration.GetConnectionString("DefaultConnection");
            } else {
               var connUrl = Environment.GetEnvironmentVariable("HEROKU_POSTGRESQL_SILVER_URL");
                connUrl = connUrl.Replace("postgres://", string.Empty);

                var pgUserPass = connUrl.Split("@")[0];
                var pgHostPortDb = connUrl.Split("@")[1];
                var pgHostPort = pgHostPortDb.Split("/")[0];

                var pgDb = pgHostPortDb.Split("/")[1];
                var pgUser = pgUserPass.Split(":")[0];
                var pgPass = pgUserPass.Split(":")[1];
                var pgHost = pgHostPort.Split(":")[0];
                var pgPort = pgHostPort.Split(":")[1];

                connStr = $"Server={pgHost};Port={pgPort};User Id={pgUser};Password={pgPass};Database={pgDb};sslmode=Prefer;Trust Server Certificate=true";
                Console.WriteLine("ConnectionString: " + connStr);
            }
        options.UseNpgsql(connStr);
        });
        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);
        services.AddAuthentication(auth => {
           auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
           auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x => {
           x.RequireHttpsMetadata = false;
           x.SaveToken = true;
           x.TokenValidationParameters = new TokenValidationParameters
           {
               ValidateIssuerSigningKey = true,
               IssuerSigningKey = new SymmetricSecurityKey(key),
               ValidateIssuer = false,
               ValidateAudience = false
           };
        });
    }
   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
         app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
        app.UseHttpsRedirection();
        app.UseMvc();
        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseAuthentication();
    }
}

这是错误

  

Microsoft.EntityFrameworkCore.Infrastructure [10403]           实体框架核心2.1.11-servicing-32099使用提供程序初始化了'VotingDBContext'   “ Npgsql.EntityFrameworkCore.PostgreSQL”,具有以下选项:无
  System.Net.Internals.SocketExceptionFactory + ExtendedSocketException   (99):无法分配请求的地址[:: 1]:5432

0 个答案:

没有答案