c# - 实体框架在播种时为另一个表中的每个外键创建一条新记录
问题描述
我正在尝试使用 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();
}
}
解决方案
尝试在您的CategoryConfiguration
课程中添加下一个方法:
public void Configure(EntityTypeBuilder<Category> builder)
{
builder
.HasMany(s => s.Products)
.WithOne(t => t.Category)
.HasForeignKey(t => t.CategoryId);
}
我不确定,但似乎系统在播种期间没有考虑您的外键。
推荐阅读
- javascript - Nuxt/content - 如何在文章页面上显示文章列表 (_slug)
- django - 迁移时的多个主键 | Django 模型
- c# - ASP.NET Core 2:2 错误无法将“System.Int32”类型的对象转换为“System.String”类型。关于登录过程
- r - 如何在不同的行中使用不同的条件进行过滤?
- java - 有什么方法可以编辑文本提示以转换为 TTS
- c++ - 使用 std::fill 用 0 填充结构的正确方法?
- django - 如何使用 django 中的 postgres 数据库使用(用户名、电子邮件、密码、年龄和性别)创建登录和注册表单?
- python - ARIMA 模型在新的 statsmodels ARIMA for python 中无法正常工作
- android - Kotlin Multiplatform 错误:指令与文件位置不匹配
- javascript - 如何从语音通道中获取用户ID