efcore icon indicating copy to clipboard operation
efcore copied to clipboard

No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

Open tmarinov opened this issue 10 years ago • 2 comments

Hi,

I am playing with ASP.NET 5 and EF7 and getting the message above in the following scenario:

  • dnvm list:
Active Version           Runtime Architecture OperatingSystem Alias
------ -------           ------- ------------ --------------- -----
  *    1.0.0-rc1-final   clr     x64          win
       1.0.0-rc1-final   clr     x86          win             default
       1.0.0-rc1-final   coreclr x64          win
       1.0.0-rc1-final   coreclr x86          win
       1.0.0-rc1-update1 clr     x64          win
       1.0.0-rc1-update1 clr     x86          win
       1.0.0-rc1-update1 coreclr x64          win
       1.0.0-rc1-update1 coreclr x86          win
  • I have two projects - Project.WebApi and Project.EF (class library)
  • EF7 is installed entirely in Project.EF (Project.WebApi have no idea about that) here is Project.EF -> project.json
{
  "version": "1.0.0-*",
  "description": "Project.EF Class Library",
  "authors": [ "" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }

  },

  "dependencies": {
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final"
  },

  "commands": {
      "ef": "EntityFramework.Commands"
   }
}
  • I removed "dotnet5.4" and added "dnxcore50" in Project.EF json file because of this issue: 1098#issue
  • Project.WebApi -> Startup -> ConfigureServices:
 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            EF.RegisterAll(services);
        }
  • Project.EF -> RegisterAll method
 public static void RegisterAll(IServiceCollection services)
        {
            var connection = @"some connection string";
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<MyDbContext>(options => options.UseSqlServer(connection));
        }
  • Executing c:...\Project.EF dnx ef migrations add InitialMigration -v and getting:
Finding DbContext classes...
Using context 'MyDbContext'.
System.InvalidOperationException: No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.
   at Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Data.Entity.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
   at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.<AddMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action)
No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

The idea is EF7 functionality to me moved into a different project where it is installed - I don;t want to have anything related to EF in Project.WebAPI. Is such scenario possible with the current dnx / EntityFramework version?

tmarinov avatar Dec 27 '15 23:12 tmarinov

Have you tried defining the connection string in your dbcontext class instead? You can override the OnConfiguring method and inject the connection information.

bestekov avatar Dec 28 '15 01:12 bestekov

Thnaks, it worked. Small note: the documentation here: (https://github.com/aspnet/EntityFramework/wiki/Configuring-a-DbContext#config-in-startup-code-default-ctor) must be updated since OnConfiguring now receives DbContextOptionsBuilder (instead DbContextOptions)

tmarinov avatar Dec 29 '15 11:12 tmarinov