【问题标题】:Entity Framework creates a new record per foreign key in another table while seeding实体框架在播种时为另一个表中的每个外键创建一条新记录
【发布时间】:2019-10-15 03:40:53
【问题描述】:

我正在尝试使用 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 类别。有谁知道怎么回事?

实体类型配置(fluent 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();
   }
}

【问题讨论】:

  • 在你的context.Categories.AddOrUpdate...系列之后,你需要做一个context.SaveChanges();,所以当你点击Phone的批量插入时,这些类别记录已经在数据库中了。

标签: c# database entity-framework-6 seeding


【解决方案1】:

尝试在CategoryConfiguration 类中添加下一个方法:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多