我研究了the help here on upgrading to aspnetcore 2.1.0

我的数据库是SQLExpress 2016SP1

我可以添加迁移,但是当我发布时

update-database

在程序包管理器控制台,我收到一个错误



连接字符串的形式为
   Server="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true
DbContext
public class ApiDbContext : IdentityDbContext<ApplicationUser>
{
    public ApiDbContext(DbContextOptions<ApiDbContext> options)
        : base(options)
    {
    }
}

上下文工厂是
public class MyContextFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
    public ApiDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
        var builder = new ConfigurationBuilder();
        builder.AddJsonFile("appsettings.json");
        var config = builder.Build();
        var connectionString = config.GetConnectionString("MyDatabase");
        optionsBuilder.UseSqlServer(connectionString);
        return new ApiDbContext(optionsBuilder.Options);
    }
}

[更新]

如果我在IDesignTimeDbContextFactory的实现内对连接字符串进行硬编码,则可以运行迁移
public class MyContextFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
    public ApiDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
        var connectionString =    "Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true";
        optionsBuilder.UseSqlServer(connectionString);
        return new ApiDbContext(optionsBuilder.Options);
    }
}

硬编码连接字符串是不可取的,因此我希望有一个更好的答案。

我不清楚为什么需要实现IDesignTimeDbContextFactory。 (如果没有它,我的迁移确实会失败)

我已经更新了Startup.cs和Progam.cs,使其与不需要执行的新生成的程序相匹配。
 public class Program
 {
   public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    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.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApiDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("MyDatabase")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApiDbContext>();

services.AddMvc()。SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
    // 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.UseDatabaseErrorPage();

        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc();
    }
}

[更新]

我更新到VS2017版本15.7.3。
当我重复问题并创建第一个迁移时,PM显示消息
The configuration file 'appsettings.json' was not found and is not optional

当我始终标记此文件副本时,那么add-migration和update-database都可以工作。

最佳答案

尝试按照This StackOverflow postTrustServerCertificate=True添加到连接字符串

关于sql-server - 证书链是由不受信任的颁发机构颁发的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50770862/

10-10 15:29