Adding DbContextOptions to Startup.cs without registering a data warehouse

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.

+7
c # dependency-injection entity-framework asp.net-core-mvc entity-framework-core
source share
3 answers

Do you enter your context into your controller or wherever you use it? I found that if you try to update the context instead of injecting it, it does not use the configuration specified in Startup.cs

+5
source share

I (still) have the same problem with EF7 and beta 4. This is my workaround in my data context:

 public class AspNetDataContext : IdentityDbContext, IDataContext { private readonly string _connectionString; public DbSet<Player> Players { get; set; } public AspNetDataContext(DbContextOptions options) { _connectionString = ((SqlServerOptionsExtension)options.Extensions.First()).ConnectionString; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(_connectionString); } } 

I extract the connection string from the parameters and use it in the OnConfiguring method. This is still not the solution we want, but I do not need to change anything in Startup.cs (everything that you described). And once that is fixed, you just need to remove the material from the data context class. And maybe someone has a different (and even better) solution to this problem.

+1
source share

EF7 has a new syntax from DBContextOptionsBuilder to EntityOptionsBuilder. The following is ready for use also for the command line.

 public class ContentContext : DbContext { public DbSet<Content> Contents { get; set; } public DbSet<ContentCategory> ContentCategories { get; set; } protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Data:DefaultConnection:ConnectionString"); } } 
-one
source share

All Articles