首页 > 解决方案 > 实体框架在播种时为另一个表中的每个外键创建一条新记录

问题描述

我正在尝试使用 EF 为数据库播种。

我有一张存放产品(电话)的Category桌子和一张区分不同类型产品的桌子。

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public DateTimeOffset? CreationDate { get; set; }
    public DateTimeOffset? UpdateDate { get; set; }

    public virtual List<IProduct> Products{ get; set; }

    public Category()
    {
        this.CreationDate = DateTimeOffset.UtcNow;
    }

}

public interface IProduct
{
    int ProductId { get; set; }
    string Brand { get; set; }
    string Model { get; set; }

    decimal? Price { get; set; }
    string Image { get; set; }

    int CategoryId { get; set; }
    Category Category { get; set; }
}

public class Phone: IProduct
{
    public int ProductId { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }
    public string network_technology { get; set; }
    public string bands_2G { get;set; }
    public string bands_3G{ get; set; }
    public string bands_4G { get; set; }
    public string network_speed { get; set; }
    public string GPRS { get; set; }
    public string EDGE { get; set; }
    public string announced { get; set; }
    public string status { get; set; }
    public string dimentions { get; set; }
    public float? weight_g { get; set; }
    public float? weight_oz { get; set; }
    public string SIM { get; set; }
    public string display_type { get; set; }
    public string display_resolution { get; set; }
    public string display_size { get; set; }
    public string OS { get; set; }
    public string CPU { get; set; }
    public string Chipset { get; set; }
    public string GPU { get; set; }
    public string memory_card { get; set; }
    public string internal_memory { get; set; }
    public string RAM { get; set; }
    public string primary_camera { get; set; }
    public string secondary_camera { get; set; }
    public string loud_speaker { get; set; }
    public string audio_jack { get; set; }
    public string WLAN { get; set; }
    public string bluetooth { get; set; }
    public string GPS { get; set; }
    public string NFC { get; set; }
    public string radio { get; set; }
    public string USB { get; set; }
    public string sensors { get; set; }
    public string battery { get; set; }
    public string colors { get; set; }
    public decimal? Price { get; set; }
    public string Image { get; set; }
}

我不知道我做错了什么,但是在我从 nuget 控制台更新数据库后,每个种子产品(电话)都会创建一个新的类别记录。这与我想要的完全相反。我希望所有电话都有一个categoryId涉及Phones类别的电话。有谁知道出了什么问题?

类别表

电话表

实体类型配置(流利的 api):

public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
    public CategoryConfiguration()
    {
        ToTable("Categories");
        HasKey(m => m.CategoryId);
    }
}

public class PhoneConfiguration : EntityTypeConfiguration<Phone>
{
    public PhoneConfiguration()
    {
        ToTable("Phones");
        HasKey(m => m.ProductId);
    }
}

种子方法:

protected override void Seed(BestPhone.Data.BestPhoneDbContext context)
{
       context.Categories.AddOrUpdate(new Category(){Name = "Phones", CategoryId = 1});
       ...
       //getting records from a csv file and holding them in an array.
       var records = csvReader.GetRecords<Phone>().ToArray();

       foreach (var record in records)
       {
           record.CategoryId = 1;
       }

       context.Phones.AddRange(records);
       context.SaveChanges();
   }
}

标签: c#databaseentity-framework-6seeding

解决方案


尝试在您的CategoryConfiguration课程中添加下一个方法:

    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder
            .HasMany(s => s.Products)
            .WithOne(t => t.Category)
            .HasForeignKey(t => t.CategoryId);
    }

我不确定,但似乎系统在播种期间没有考虑您的外键。


推荐阅读