Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Migration not working using Core

$
0
0

Hi,

Running migration, "Update-Database -Context ApplicationContext", why do I get this error at console "Value cannot be null".

At OnConfiguring method, the DB connection string is stored to a variable hence the error occurs. 

appsettings.json

{"ConnectionStrings": {"DefaultConnection": "Server=(LocalDB)\\MSSQLLocalDB;Database=DBName;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}


ApplicationContext.cs

public class ApplicationContext : DbContext
{
public IConfigurationRoot Configuration { get; }

public ApplicationContext()
{
}

public ApplicationContext(DbContextOptions<ApplicationContext> options)
: base(options)
{
}

public DbSet<ModelEntity> ModelEntity { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DBName;Trusted_Connection=True;"); <<---working
var connectionString = Configuration.GetConnectionString("DefaultConnection");

optionsBuilder.UseSqlServer(connectionString); <<---NOT Working
}
}


However, if I implicitly insert the connectionstring like below then the migration works fine.

optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DBName;Trusted_Connection=True;");

Why the implicit inserts works?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>