【问题标题】:Can't add Seed data to database tables?无法将种子数据添加到数据库表?
【发布时间】:2021-04-15 12:24:51
【问题描述】:

当我想将种子数据添加到数据库时,我得到了这个错误并且我无法解决它。表具有多对多关系。 我在下面附上了我的表格和上下文文件。你能帮帮我吗?

无法添加实体类型“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", }
                 );
            }
        }

这是我建立多对多关系的表。

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

        }
    }
}

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

【问题讨论】:

    标签: asp.net-core entity-framework-core ef-core-3.1


    【解决方案1】:

    这是因为你有两个类来播种同一个实体“BasicCategory” 尝试结合 MainCategorySeed 和 CategorySeed 再试一次

    【讨论】:

    • 对不起,我写错了。请看一下我更正的代码。问题可能是我没有定义外键和主键吗?
    • 您在同一种子“CategorySeed”的两条记录中定义了相同的 BasicCategoryId = 1,这意味着您尝试定义具有相同 id 的两个基本类别,更改第二条记录 BasicCategoryId = 2 并重试
    • 我不明白。我在哪里创建了两条记录? (ProductCategory关系表)
    • 这里 class CategorySeed:IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.HasData( new BasicCategory { BasicCategoryId = 1, Name = "ELEKTRONİK&BEYAZ EŞYA", }, new BasicCategory { BasicCategoryId = 1, 名称 = "MODA", } ); } } 更改第二个种子记录 BasicCategoryId = 2
    • 我按照它说的做了,然后我得到了这个错误,这次我得到了这个错误 INSERT 语句与 FOREIGN KEY 约束“FK_ProductCategory_SubCategories_SubCategoryId”冲突。冲突发生在数据库“BuyfiletDb”、表“dbo.SubCategories”、列“SubCategoryId”中。声明已终止。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2020-01-04
    • 2011-05-11
    相关资源
    最近更新 更多