添加种子数据时出现以下错误::'No database provider has been configured for this DbContext

Posted

技术标签:

【中文标题】添加种子数据时出现以下错误::\'No database provider has been configured for this DbContext【英文标题】:When adding seed data I get the following error::'No database provider has been configured for this DbContext添加种子数据时出现以下错误::'No database provider has been configured for this DbContext 【发布时间】:2021-07-13 01:31:59 【问题描述】:

我收到此错误:

可以通过覆盖“*DbContext.OnConfiguring”方法或在应用程序服务提供者上使用“AddDbContext”来配置提供者。如果使用了“AddDbContext”,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数

种子文件和数据库文件如下。

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]
    ;

数据库文件:

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);
    

【问题讨论】:

能否请您也发布您的启动文件? @sedat:查看以下帖子:'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync 【参考方案1】:

删除默认构造函数并使用documentation中描述的以下代码;

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

using var context = new ApplicationDbContext(contextOptions);

删除默认构造函数的原因是您的 DbContext 必须知道将连接到哪里以及如何连接到数据库。不要忘记使用“using”语句来更好地管理内存。

【讨论】:

我做到了。但是现在调用数据库product.ProductCategory中的数据时显示为空。但所有种子数据都在数据库中可用 您可以在模型创建或启动时调用您的 Seed 方法。

以上是关于添加种子数据时出现以下错误::'No database provider has been configured for this DbContext的主要内容,如果未能解决你的问题,请参考以下文章

使用 KivyMD 库运行应用程序时出现错误 No module named 'kivymd.app'

删除记录时出现 NO_SQL_DATA 错误,firedac,delphi 10.3.1

连接数据库时出现No suitable driver found的错误是啥意思

添加ORDER BY时出现“SQL0802 - 数据映射错误的数据转换”异常

移动光标时出现 android.database.CursorWindowAllocationException

当安装INSTALL_FAILED_NO_MATCHING_ABIS时出现Android AIR 33错误:无法提取本机库,res = -113