31

I studied the help here on upgrading to aspnetcore 2.1.0

My database is SQLExpress 2016SP1

I am able to add a migration but when I issue

update-database

at the Package Manager Console, I get an error

A connection was successfully established with the server, but then an error occurred during the login process.
(provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

The connection string is of the form

   Server="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True;MultipleActiveResultSets=true

The DbContext is

public class ApiDbContext : IdentityDbContext<ApplicationUser>
{
    public ApiDbContext(DbContextOptions<ApiDbContext> options)
        : base(options)
    {
    }
}

The Context Factory is

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

[Update]

If I hard code the connection string inside the implementation of IDesignTimeDbContextFactory then I can run the migrations

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

Hard coding the connection string is not desirable so I would like a better answer.

I am unclear as to why the implementation of IDesignTimeDbContextFactory is needed. (my migration does fail without it )

I have updated my Startup.cs and Progam.cs to match those of a newly generated program where the implementation is not needed.

 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();
    }
}

[Update]

I updated to VS2017 version 15.7.3. When I went to repeat the problem and create the first migration, PM displayed the message

The configuration file 'appsettings.json' was not found and is not optional

When I marked this file copy always then both the add-migration and the update-database worked.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • You likely don't have a CA-signed certificate installed in your SQL – Vivek Nuna Jun 09 '18 at 04:41
  • 1
    @viveknuna I don't need a certificate for a new project so why would I need one for an upgraded project? – Kirsten Jun 10 '18 at 08:51
  • If using Windows Authentication is an option, along with doing what @Will Jones said(setting TrustServerCertificate=True). This is what worked for me. – Danimal111 Feb 26 '19 at 20:09

4 Answers4

81

Try adding TrustServerCertificate=True to the connection string, as per This StackOverflow post

2022 Update:

The linked post has been updated with more up to date recommendations. The answer posted here is a stopgap, and not recommended for production workloads

Will Jones
  • 1,861
  • 13
  • 24
7

Setting "TrustServerCertificate=True" and using Windows Authentication resolved this for me.

Danimal111
  • 1,976
  • 25
  • 31
0
"Server=.\\SQLEXPRESS;Database=Bulky;TrustServerCertificate=True;User ID=sa; Password=123;Trusted_Connection=True"

replace Password with your selected password and user

Express SQL installed locally

F. Müller
  • 3,969
  • 8
  • 38
  • 49
0

changing Encrypt=False; in the connection string worked for me;

Khalid
  • 343
  • 3
  • 16