问题描述
我收到此错误:
可以通过覆盖“*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,ProductId = 2,StokKodu = "5465465456",Detaylari =
"Samsung galaxy M51 128 GB (Samsung Türkiye Garantili Samsung galaxy M51 128 GB (Samsung Türkiye Garantili",};
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});
}
}
解决方法
删除默认构造函数并使用以下代码,如documentation;
var contextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test")
.Options;
using var context = new ApplicationDbContext(contextOptions);
删除默认构造函数的原因是您的 DbContext 必须知道将连接到哪里以及如何连接到数据库。不要忘记使用“using”语句来更好地管理内存。