I have ASP.Net Core 2.1 with EF Core 2.1. This is how my DbContext class looks like
app.DAL.EF -> Layer
using app.domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace app.EF
{
public class MyAppContext : DbContext
{
public MyAppContext(DbContextOptions<MyAppContext> options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
modelBuilder.HasDefaultSchema("app");
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
public DbSet<Customer> Customers { get; set; }
}
public class MyAppContextConfiguration : IDesignTimeDbContextFactory<MyAppContext>
{
public MyAppContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ") ?? "Production" }.json", optional: true)
.Build();
var optionsBuilder = new DbContextOptionsBuilder<MyAppContext>();
//optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
var dbConString = configuration.GetConnectionString("ITMDbConnection");
optionsBuilder.UseSqlServer(dbConString);
return new MyAppContext(optionsBuilder.Options);
}
}
public class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.HasKey(x => x.Id);
}
}}
app.DI -> Layer
public static class Factory
{
public static void Initialize(ref IServiceCollection services)
{
//services.AddTransient<MyAppContext>();
services.AddDbContext<MyAppContext>(options =>
{
});
//services.AddTransient<MyAppContextConfiguration>();
services.AddTransient<ICustomerRepository, CustomerRepository>();
}
}
app.API -> Layer
namespace app.api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Factory.Initialize(ref services);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}}
When running Add-Migration DbInit
from Package Manager Console, throwing the below error
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 accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
Thanks!
AddDbContext
. Which, btw, should be inStartup.cs
. Those methods are DI configuration methods, not factories or layers. If you want to simplify your code you could create an extension method that receives and returns anIServiceCollection
, alowing you to chain config calls just like other DI config methods – Panagiotis KanavosMyAppContextConfiguration
class? – Kgn-webCreateDbContext
anywhere. This code is a bit overcomplicated. Those classes, methods aren't layers, they are just a DbContext and some startup config methods. The DbContext configuration and DI registration is performed byAddDbContext
. A simpleoptions.UseSqlServer(Configuration.GetConnectionString("ITMDbConnection"))
in there would work. TheMyAppContextConfiguration
class isn't used anywhere in Startup.cs – Panagiotis KanavosCreateDbContext
- loadappsettings.json
, any environment files etc. This means that theConfiguration
field inStartup.cs
can load the connection strings – Panagiotis Kanavosoptions.UseSqlServer()
in Factory or Startup class. Then tight coupling is being introduced – Kgn-web