Entity Framework Core Migrations reading connection from appsettings.json

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

Question

I'm having issues reading a connection string from an appsettings.json file.

I'm using a .netstandard2.0 .dll in which I have EfCore packages and Configuration

enter image description here

The sln structure is as following.

I have an webApi , an EfCore.dll and some DI.dll that resolves all dependencies.

When running in the package manager console (from EfCore assembly)

Add-Migration Name

I have the following error

Value cannot be null.

Parameter name: connectionString

This is the class that I've implemented in order for Migrations to work. following this article https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation

public class StoreContextFactory : IDesignTimeDbContextFactory<StoreContext>
    {
        IConfiguration Configuration = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
              .Build();

        public StoreContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<StoreContext>();
            optionsBuilder.UseSqlServer(Configuration["ConnectionString:SqlConnection"]);

            return new StoreContext(optionsBuilder.Options);
        }
    }

In appsettings.json I have the

 "ConnectionString": {
    "SqlConnection": "connection"
  }

When instead of Configuration["ConnectionString:SqlConnection"] I use the connection it works but when using the Configuration extension class it fails.

Any solution?

Thanks

1
0
9/21/2018 2:23:55 PM

Popular Answer

The only reason you really might need IConfigurationRoot is if you want to reload config, or want access to the individual providers that make up the config. So just use IConfiguration instead:

public class StoreContextFactory : IDesignTimeDbContextFactory<StoreContext>
{
    private IConfiguration _configuration;

    public StoreContextFactory()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        _configuration = builder.Build();
    }

    public StoreContext CreateDbContext(string[] args)
    {
        DbContextOptionsBuilder<StoreContext> optionsBuilder = new DbContextOptionsBuilder<StoreContext>();

        optionsBuilder.UseSqlServer(_configuration.GetConnectionString("YourConnectionName"));

        return new StoreContext(optionsBuilder.Options);
    }
}

GetConnectionString is an extention method which does this:

public static string GetConnectionString(this IConfiguration configuration, string name)
{
  return configuration?.GetSection("ConnectionStrings")?[name];
}
0
9/23/2018 7:32:08 PM


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