Entity Framework Core - 'Update-Database' not working

asp.net-core c# entity-framework-core

Question

In my solution I have several .NET Core 2.0 projects:

  • App.DataAccess
  • App.DataAccess.Migrations
  • App.WebApi

In App.DataAccess.Migrations (which I want to use as the migrations assembly) I have an AppDbContextFactory class:

namespace App.DataAccess.Migrations
{
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Design;
    using Microsoft.Extensions.Configuration;
    using System.IO;

    public sealed class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
    {
        public AppDbContextFactory()
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
        }

        public IConfiguration Configuration { get; }

        public AppDbContext CreateDbContext(string[] args)
        {
            DbContextOptionsBuilder<AppDbContext> builder = new DbContextOptionsBuilder<AppDbContext>();

            builder.UseSqlServer(
                connectionString: Configuration.GetConnectionString("DefaultConnection"),
                sqlServerOptionsAction: sqlOptions => sqlOptions.MigrationsAssembly(GetType().Namespace));

            return new AppDbContext(builder.Options);
        }
    }
}

In the same project, I also have an appsettings.json file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=MyApp;User ID=sa;Password=SomethingSecret;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

In my App.WebApi project, I have the following in my Startup class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(ConfigureDatabaseOptions);
    services.AddMvc();
}

private void ConfigureDatabaseOptions(DbContextOptionsBuilder builder)
{
    builder.UseSqlServer("DefaultConnection", ConfigureSqlServerDatabaseOptions);
}

private void ConfigureSqlServerDatabaseOptions(SqlServerDbContextOptionsBuilder builder)
{
    builder.MigrationsAssembly(typeof(AppDbContextFactory).Namespace);
}

In the same project, I've also added the connection string to appsettings.json.

Add-Migration works.

Update-Database throws this error:

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Why?

1
2
4/6/2018 1:09:21 PM

Accepted Answer

Solved it. I'm passing "DefaultConnection" as the connection string instead of the connection string name:

private void ConfigureDatabaseOptions(DbContextOptionsBuilder builder)
{
    builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), ConfigureSqlServerDatabaseOptions);
}
4
1/28/2018 1:38:16 PM

Popular Answer

The way I could solve the problem was in the following way, in order to understand the solution I placed the name of a fictitious connectionString. it´s solution

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AuthenticDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
    services.AddIdentity<AplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<AuthenticDBContext>()
        .AddDefaultTokenProviders();
    services.AddMvc().AddJsonOptions(configureJson);
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow