My problem is that the code below does not register the data store at startup. This is the specific βerrorβ expression that I get in the response from the application:
An unhandled exception occurred while processing the request. InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore(ServiceProviderSource providerSource)
In ConfigureServices (IServiceCollection service) I am trying to specify DbContextOptions for my DbContext in lambda. The code:
services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext<MyDbContext>( options => options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")) );
In my DbContext, I have a constructor that sends an option to the base, the code:
public MyContext(DbContextOptions options) : base(options) { }
My config.json configuration file, which is read at startup, contains this connection string:
"Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MyDbName;Trusted_Connection=True;MultipleActiveResultSets=True;" } }
I previously used
protected override void OnConfiguring(DbContextOptions options) { options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString")); }
in my DbContext successfully. It registers the data store and works correctly, but I prefer to use a lambda image.
If you need more information, I have provided it.
c # dependency-injection entity-framework asp.net-core-mvc entity-framework-core
DanielRJ
source share