Büşra Nur
Büşra Nur

Reputation: 11

How to fix the problem"More than one DbContext was found. Specify which one to use." when I migrate in C# ASP.NET Core MVC web app?

I have two DbContext, how to integrate them? First is in areas folder, I added Identity from the new scaffold page, but when I also try to execute IIS Express, I get an error

Some services are not able to be constructed

The second DbContext class in the areas folder:

public class EcommDbContext : IdentityDbContext<ApplicationUser>
{
    public EcommDbContext(DbContextOptions<EcommDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

First DbContext class in the Data folder:

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
    }

    public DbSet<Account> Accounts => Set<Account>();
    public DbSet<Address> Addresses => Set<Address>();
    public DbSet<AddressNotebook> AddressNotebooks => Set<AddressNotebook>();
    public DbSet<Basket> Baskets => Set<Basket>();
    public DbSet<CartItem> CartItems => Set<CartItem>();
    public DbSet<Category> Categories => Set<Category>();
    public DbSet<Contact> Contacts => Set<Contact>();
    public DbSet<CreditCard> CreditCards => Set<CreditCard>();
    public DbSet<Customer> Customers => Set<Customer>();
    public DbSet<CustomerPayment> CustomerPayments => Set<CustomerPayment>();
    public DbSet<Discount> Discounts => Set<Discount>();
    public DbSet<EditAcc> EditAccs => Set<EditAcc>();
    public DbSet<Location> Locations => Set<Location>();
    public DbSet<MainPage> MainPages => Set<MainPage>();
    public DbSet<MyAccount> MyAccounts => Set<MyAccount>();
    public DbSet<MyInfo> MyInfos => Set<MyInfo>();
    public DbSet<OrderDetail> OrderDetails => Set<OrderDetail>();
    public DbSet<OrderItem> OrderItems => Set<OrderItem>();
    public DbSet<OrderPast> OrderPasts => Set<OrderPast>();
    public DbSet<PaymentDetail> PaymentDetails => Set<PaymentDetail>();
    public DbSet<Product> Products => Set<Product>();
    public DbSet<ShoppingCart> ShoppingCarts => Set<ShoppingCart>();
}

appsettings.json:

"ConnectionStrings": {
    "EcommDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=Ecomm;Trusted_Connection=True;MultipleActiveResultSets=true"
}

Program.cs:

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("EcommDbContextConnection") ?? throw new InvalidOperationException("Connection string 'EcommDbContextConnection' not found.");

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

builder.Services.AddDbContext<DataContext>(options =>
{
    var config = builder.Configuration;
    var connectionString = config.GetConnectionString("database");
    options.UseSqlServer(connectionString);
});

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<EcommDbContext>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.MapRazorPages();

AppDbInitializer.Seed(app);

app.Run();

I am trying to add login-register page for an e-commerce project in ASP.NET Core MVC, I am trying to add every feature by myself. I was expecting for it to work properly but it gives errors. I can edit and share other missing details for this project if the question details were not given fully.

Upvotes: 0

Views: 82

Answers (0)

Related Questions