2

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?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

2 Answers2

4

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);
}
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
1

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);
        }
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • AppSetting.Json "ConnectionStrings": { "ConnectionString": "Server=Server;Database=Database;Trusted_Connection=True;MultipleActiveResultSets=true" }, – Flavio Cortes Jul 06 '18 at 14:17