无法将 Seed 数据添加到数据库表中?

Can't add Seed data to database tables?

当我想将种子数据添加到数据库时,出现此错误,我无法解决。表具有 many-to-many 关系。 我在下面附上了我的 tables 和上下文文件。你能帮帮我吗?

无法添加实体类型 'BasicCategory' 的种子实体,因为已添加具有与 {'BasicCategoryId'} 相同键值的另一个种子实体.考虑使用 'DbContextOptionsBuilder.EnableSensitiveDataLogging' 查看冲突的键值

    namespace BuyfiletData.Concrete.EfCore.Seeds
    {
        class ProductSeed:IEntityTypeConfiguration<Product>
        {
            public void Configure(EntityTypeBuilder<Product> builder)
            {
                builder.HasData(
                    new Product {Id=1, StockCode="5484959", Label="Samsung S6", Status=1, Brand= "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili)",  IsOptionedProduct=1, IsOptionOfAProduct=2400, MarketPrice=2600, BuyingPrice="2400", Price=2400, Tax=14, CurrencyAbbr="tl", RebateType=1, Rebate=200, MoneyOrder=300, StockAmount=5, StockType="cm", Warranty=200, Picture1Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture2Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture3Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture4Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture5Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture6Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Dm3 = 2554, Details = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili", Point = 15.8, Variety = "kırmızı", Size = "25", Color = "siyah", Gender = "erkek",},
                    new Product {Id=2, StockCode="5465465456", Label="Samsung S6", Status=1, Brand= "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili)",  IsOptionedProduct=1, IsOptionOfAProduct=2400, MarketPrice=2600, BuyingPrice="2400", Price=2400, Tax=14, CurrencyAbbr="tl", RebateType=1, Rebate=200, MoneyOrder=300, StockAmount=5, StockType="cm", Warranty=200, Picture1Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture2Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture3Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture4Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture5Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture6Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Dm3 = 2554, Details = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili", Point = 15.8, Variety = "kırmızı", Size = "25", Color = "siyah", Gender = "erkek",},
                    new Product {Id=3, StockCode="5484959", Label="Samsung S6", Status=1, Brand= "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili)", IsOptionedProduct=1, IsOptionOfAProduct=2400, MarketPrice=2600, BuyingPrice="2400", Price=2400, Tax=14, CurrencyAbbr="tl", RebateType=1, Rebate=200, MoneyOrder=300, StockAmount=5, StockType="cm", Warranty=200, Picture1Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture2Path= new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture3Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture4Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture5Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Picture6Path = new Uri("https://productimages.hepsiburada.net/s/43/550/10756209672242.jpg/format:webp"), Dm3 = 2554, Details = "Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili Samsung Galaxy M51 128 GB (Samsung Türkiye Garantili", Point = 15.8, Variety = "kırmızı", Size = "25", Color = "siyah", Gender = "erkek",},
         
                );
            }
        }
    }
-----
    class ProductCategorySeed:IEntityTypeConfiguration<ProductCategory>
    {
        public void Configure(EntityTypeBuilder<ProductCategory> builder)
        {
            
            
            builder.HasData(
                new ProductCategory { BasicCategoryId = 1,MainCategoryId = 1,SubCategoryId = 1,ProductId = 1},
                new ProductCategory { BasicCategoryId = 1,MainCategoryId = 1,SubCategoryId = 2,ProductId = 2},
                new ProductCategory { BasicCategoryId = 1,MainCategoryId = 1,SubCategoryId = 3,ProductId = 3},
            
            );
        }
    }

----

        public void Configure(EntityTypeBuilder<MainCategory> builder)
    {
        builder.HasData(
            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" }
           
        );
    }
                  
                   
                );
            }
        }
---
 

    class CategorySeed:IEntityTypeConfiguration<BasicCategory>
        {
            public void Configure(EntityTypeBuilder<BasicCategory> builder)
            {
    
             builder.HasData(
                 new BasicCategory { BasicCategoryId = 1, Name = "ELEKTRONİK&BEYAZ EŞYA", },
                 new BasicCategory { BasicCategoryId = 1, Name = "MODA", }
                 );
            }
        }

这是我建立 many-to-many 关系的 table。

   public class ProductCategory
    {
        public int BasicCategoryId { get; set; }
        public int ProductId { get; set; }
        public int MainCategoryId { get; set; }
        public int SubCategoryId { get; set; }
        public Product Product { get; set; }
        public SubCategory SubCategory { get; set; }
        public BasicCategory BasicCategory { get; set; }
        public MainCategory MainCategory { get; set; }
    }

这是我的上下文文件

using AlisverisagiEntity;
using AlisverisagiEntity.EntityModels;
using BuyfiletData.Concrete.EfCore.Seeds;
using BuyfiletEntity.bagla;
using BuyfiletEntity.EntityModels.Category;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BuyfiletData.Concrete.EfCore
{
    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<UserEntity> UserEntities { get; set; }

     

      

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<MainCategory>().HasKey(x => x.Id);
            
            modelBuilder.Entity<ProductCategory>().HasKey(c => new
            {c.BasicCategoryId, c.ProductId,c.MainCategoryId,c.SubCategoryId}); 
            
            
           modelBuilder.ApplyConfiguration(new ProductSeed());
            modelBuilder.ApplyConfiguration(new CategorySeed());
            modelBuilder.ApplyConfiguration(new MainCategorySeed());
            modelBuilder.ApplyConfiguration(new ProductCategorySeed());
            
            modelBuilder.Entity<Connect>().HasKey(c => new
            {
                c.ChildsId,
                c.GrandchildsId,
                c.UserEntityId,
            });

        }
    }
}

我遇到了标题中提到的错误。我该如何解决?

这是因为您有两个 类 来为同一个实体“BasicCategory”设置种子 尝试合并 MainCategorySeed 和 CategorySeed 再试一次