Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

asp.net core - Can't add Seed data to database tables?

When I want to add seed data to the database, I get this error and I couldn't solve it. Tables have many-to-many relationships. I have attached my tables and context file below. Can you help me?

The seed entity for entity type 'BasicCategory' cannot be added because another seed entity with the same key value for {'BasicCategoryId'} has already been added. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values

    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", }
                 );
            }
        }

This is the table where I establish the many-to-many relationship.

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

This is my context file

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

        }
    }
}

I am getting the error I mentioned in the title. How can I fix?

question from:https://stackoverflow.com/questions/65651977/cant-add-seed-data-to-database-tables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's because you have two classes to seed same entity "BasicCategory" Try to Combine MainCategorySeed and CategorySeed and try again


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...