How to fix error Add-Migration connectionstring null

1

The message is the same despite the fact that I saw other similar questions. Later I put other images to see how the appsettings and context are.

appsettings.json.

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=RACPAVI\RAC; Initial Catalog=WEBAPIPAIS; UserID=sa; Password=qpwoei"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

ApplicationDbContext.cs

public class ApplicationDbContext: DbContext
{
    public ApplicationDbContext()
    {
    }
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        :base(options)
    {

    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        IConfigurationRoot config = builder.Build();
        optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));
    }
    public DbSet<Pais> Paises { get; set; }
    public DbSet<Provincia> Provincias { get; set; }
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        var connectionString = configuration.GetConnectionString("DefaultConnection");
        builder.UseSqlServer(connectionString);
        return new ApplicationDbContext(builder.Options);
    }
}
    
asked by A Robert 02.10.2018 в 07:56
source

2 answers

1

In this line you should not put the complete connection string, just the name of it in the configuration file. And in the configuration file it respects the spaces and the connection string in the same row.

OptionBuilder.UseSQLServer(config.GetconnectionString("Defaultconnection"));

Verify that you are taking the loading process of the configuration file well

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
                     .AddJsonFile("appsettings.json", optional: true,
                                   reloadOnChange: true);


IConfigurationRoot configurationRoot = configurationBuilder.Build();

and then

var cs = configurationRoot.GetConnectionString("DefaultConnection");
configurationBuilder.UseSqlServer(cs);
    
answered by 02.10.2018 в 18:37
0

Why do you first call the father's event and with the parameter and then load it? base.OnConfiguring(optionsBuilder); in fact you should not call it here.

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        IConfigurationRoot config = builder.Build();
        optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));

    }
    
answered by 04.10.2018 в 19:10