我研究过the help here on upgrading to aspnetcore 2.1.0
我的数据库是SQLExpress 2016SP1
我可以添加迁移,但当我发出
update-database
在Package Manager控制台上,我收到一个错误
成功建立了与服务器的连接,但随后在登录过程中发生错误。
(提供程序: SSL提供程序,错误:0-证书链由不受信任的颁发机构颁发。)
连接字符串的格式为
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都工作了。
发布于 2018-06-12 00:35:55
尝试按照This StackOverflow post向连接字符串添加TrustServerCertificate=True
发布于 2019-02-27 04:12:16
设置"TrustServerCertificate=True“并使用Windows Authentication为我解决了这个问题。
https://stackoverflow.com/questions/50770862
复制相似问题