0
votes

I am get this error:

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

Seed file and database file are below.

public static class SeedDatabase
{
    public static void Seed()
    {
        var context = new ShopContext();
        context.MainCategories.AddRange(mainCategories);
        context.SubCategories.AddRange(subCategories);
        context.Categories.AddRange(basicCategories);
        context.Products.AddRange(products);
        context.AddRange(productCategories);

        context.SaveChanges();
    }

    private static BasicCategory[] basicCategories =
    {
        new BasicCategory() {BasicCategoryId = 1, Name = "ELEKTRONİK&BEYAZ EŞYA",}, new BasicCategory() {BasicCategoryId = 2, Name = "MODA",}
    };

    private static MainCategory[] mainCategories =
    {
        new MainCategory() {MainCategoryId = 1, Name = "Cep Telefonu ve Aksesuar"}, new MainCategory() {MainCategoryId = 2, Name = "Bilgisayar, Tablet"}, 
        new MainCategory() {MainCategoryId = 3, Name = "Erkek"}, new MainCategory() {MainCategoryId = 4, Name = "Kadın"}
    };

    private static SubCategory[] subCategories =
    {
        new SubCategory() {SubCategoryId = 1, Name = "Cep Telefonu"}, new SubCategory() {SubCategoryId = 2, Name = "Cep Telefonu Aksesuar"},
        new SubCategory() {SubCategoryId = 3, Name = "Kılıf"}, new SubCategory() {SubCategoryId = 4, Name = "Şarj Cihazı"},
    };

    private static Product[] products =
    {
        new Product
        {
            
            Puani = 4.4, KargoUcretsizmi = true, PuanAdedi = 89, ProductId = 1, StokKodu = "5484959", Label = "Samsung S6", Durumu = "Sıfır", Markasi = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili)", BirkacFarklıSecenegiVarMı = true,
            PiyasaFiyati = 2600, AlisFiyati = 2400, Fiyat = 2400, KDVOrani = 14, ParBirimi = "tl",
            IndirimliFiyatMi = true, Indirim = 200, HavaleIndirimOrani = 300, StokAdedi = 5, StokTipi = "cm",
            GarantiSuresi = 200,
            Resim1 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim2 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim3 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim4 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim5 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim6 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Detaylari = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili", AnasayfadaMi = true, UrunAktifMi = true
        },
        new Product
        {
            Puani = 4.4, KargoUcretsizmi = true, PuanAdedi = 89, ProductId = 2, StokKodu = "5465465456", Label = "Samsung S6", Durumu = "Sıfır",
            Markasi = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili)", BirkacFarklıSecenegiVarMı = true,
            PiyasaFiyati = 2600, AlisFiyati = 2400, Fiyat = 2400, KDVOrani = 14, ParBirimi = "tl", IndirimliFiyatMi = true, Indirim = 200, HavaleIndirimOrani = 300, StokAdedi = 5, StokTipi = "cm", GarantiSuresi = 200,
            Resim1 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim2 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim3 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim4 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim5 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp", Resim6 = "https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp",
            Detaylari =
                "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili",
            AnasayfadaMi = true, UrunAktifMi = true
        },
    };

    private static ProductCategory[] productCategories =
    {
        new ProductCategory(){BasicCategory = basicCategories[1],MainCategory = mainCategories[1],SubCategory = subCategories[1],Product = products[0]}
    };
}

Database file:

public class ShopContext:IdentityDbContext<UserEntity>
{
    public ShopContext()
    {
    }

    public ShopContext(DbContextOptions<ShopContext> options) : base(options)
    {
    }
 
    public DbSet<UserEntity> UserModels { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<BasicCategory> Categories { get; set; }
    public DbSet<MainCategory> MainCategories { get; set; }
    public DbSet<SubCategory> SubCategories { get; set; }

    public DbSet<Cart> Carts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Product>().HasKey(x => x.ProductId);
        modelBuilder.Entity<Product>().Property(x => x.ProductId).UseIdentityColumn();
        
        modelBuilder.Entity<BasicCategory>().HasKey(x => x.BasicCategoryId);
        modelBuilder.Entity<BasicCategory>().Property(x => x.BasicCategoryId).UseIdentityColumn();
        
        modelBuilder.Entity<MainCategory>().HasKey(x => x.MainCategoryId);
        modelBuilder.Entity<MainCategory>().Property(x => x.MainCategoryId).UseIdentityColumn();

        modelBuilder.Entity<SubCategory>().HasKey(x => x.SubCategoryId);
        modelBuilder.Entity<SubCategory>().Property(x => x.SubCategoryId).UseIdentityColumn();

        modelBuilder.Entity<ProductCategory>().HasKey(c => new {c.BasicCategoryId, c.ProductId,c.MainCategoryId,c.SubCategoryId});
    }
}
1
Could you post your startup file as well pls?Serge

1 Answers

0
votes

Remove the default contructor and use the following code like described in documentation;

var contextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test")
.Options;

using var context = new ApplicationDbContext(contextOptions);

The reason for deletion of default constructor is your DbContext must know where will connect and how will connect to database. Don't forget to use the "using" statement for better memory management.