Reputation: 675
I am using EF6 and I have generated my dbcontext, but vs recommends me not to have the connection string within the same code, so I passed it to the appsettings.json and I am reading it from my Program.cs but I don't know how to do that my DBContext class knows that the connection string has already been set.
First, i remove the connection string config from OnConfiguring method from my dbcontext class:
Then y add my config in Program class:
but when i execute a database call it throws me the following exception:
No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type
How do I make my DBContext class know that the connection string is being read? What should I put in my OnConfiguring method?
Upvotes: 0
Views: 257
Reputation: 135
First of all, you need to pass the following line of code into your OnConfiguring
method:
base.OnConfiguring(optionsBuilder);
In situations where an instance of DbContextOptions
may or may not have been passed to the constructor, you can use DbContextOptionsBuilder.IsConfigured
to determine if the options have already been set, and skip some or all of the logic in OnConfiguring(DbContextOptionsBuilder)
.
Upvotes: -1