C# 使用对象集合进行EF核心种子设定
我需要用种子数据填充数据库(SQL server) 对于“实习”表,我试图输入名为DesiredSkills的集合,但在运行迁移时,我收到以下错误消息:“对象引用未设置为对象的实例。” 是否可以使用列表之类的集合为数据库种子 DbContext:C# 使用对象集合进行EF核心种子设定,c#,entity-framework-core,seeding,C#,Entity Framework Core,Seeding,我需要用种子数据填充数据库(SQL server) 对于“实习”表,我试图输入名为DesiredSkills的集合,但在运行迁移时,我收到以下错误消息:“对象引用未设置为对象的实例。” 是否可以使用列表之类的集合为数据库种子 DbContext: public InternshipDbContext(DbContextOptions<InternshipDbContext> options) : base(options) { } p
public InternshipDbContext(DbContextOptions<InternshipDbContext> options) : base(options)
{
}
public DbSet<Avatar> Avatars { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<Internship> Internships { get; set; }
public DbSet<Login> Logins { get; set; }
public DbSet<Skill> Skills { get; set; }
public DbSet<Trainee> Trainees { get; set; }
public DbSet<TraineeRequest> TraineeRequests { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SharedTypeEntity<Dictionary<string, object>>("InternshipSkill");
modelBuilder.Entity<Trainee>()
.HasOne(x => x.Login)
.WithMany()
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Internship>()
.HasOne(x => x.Company)
.WithMany()
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.ApplyConfiguration(new LoginConfiguration());
modelBuilder.ApplyConfiguration(new AvatarConfiguration());
modelBuilder.ApplyConfiguration(new CompanyConfiguration());
modelBuilder.ApplyConfiguration(new TraineeConfiguration());
modelBuilder.ApplyConfiguration(new SkillConfiguration());
modelBuilder.ApplyConfiguration(new InternshipConfiguration());
modelBuilder.ApplyConfiguration(new TraineeRequestConfiguration());
}
公共实习生DBContext(DbContextOptions):基本(选项)
{
}
公共DbSet化身{get;set;}
公共数据库集公司{get;set;}
公共数据库集实习{get;set;}
公共数据库集登录{get;set;}
公共数据库集技能{get;set;}
公共数据库集{get;set;}
公共数据库集TrainerRequests{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.SharedTypeEntity(“实习技能”);
modelBuilder.Entity()
.HasOne(x=>x.Login)
.有很多
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity()
.HasOne(x=>x.Company)
.有很多
.OnDelete(DeleteBehavior.NoAction);
ApplyConfiguration(新的LoginConfiguration());
ApplyConfiguration(新的AvatarConfiguration());
modelBuilder.ApplyConfiguration(新公司配置());
modelBuilder.ApplyConfiguration(新的TraineConfiguration());
ApplyConfiguration(新的SkillConfiguration());
ApplyConfiguration(新的实习生配置());
modelBuilder.ApplyConfiguration(新的TrainerRequestConfiguration());
}
实习配置:
public void Configure(EntityTypeBuilder<Internship> builder)
{
builder.HasData(
new Internship { Id = 1, CompanyId = 1, CreationDate = DateTime.Now, Title = "Stage Software Developer", StartDate = DateTime.Now.AddDays(30), EndDate = DateTime.Now.AddDays(60), DesiredSkills = { new Skill { Id = 1, Name = "C#" }, new Skill { Id = 2, Name = "SQL" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink1, ShortDescription = "Wij zijn op zoek naar een schitterende Software Developer" },
new Internship { Id = 2, CompanyId = 2, CreationDate = DateTime.Now, Title = "Stage Android Developer", StartDate = DateTime.Now.AddDays(32), EndDate = DateTime.Now.AddDays(62), DesiredSkills = { new Skill { Id = 1, Name = "C#" }, new Skill { Id = 10, Name = "Xamarin" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink2, ShortDescription = "Wij zijn op zoek naar een schitterende Android Developer" },
new Internship { Id = 3, CompanyId = 3, CreationDate = DateTime.Now, Title = "Stage Angular Developer", StartDate = DateTime.Now.AddDays(35), EndDate = DateTime.Now.AddDays(65), DesiredSkills = { new Skill { Id = 1, Name = "C#" }, new Skill { Id = 4, Name = "Angular" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink3, ShortDescription = "Wij zijn op zoek naar een schitterende Angular Developer" },
new Internship { Id = 4, CompanyId = 3, CreationDate = DateTime.Now, Title = "Stage Java Developer", StartDate = DateTime.Now.AddDays(35), EndDate = DateTime.Now.AddDays(65), DesiredSkills = { new Skill { Id = 7, Name = "Java" }, new Skill { Id = 2, Name = "SQL" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink4, ShortDescription = "Wij zijn op zoek naar een schitterende Java Developer" },
new Internship { Id = 5, CompanyId = 4, CreationDate = DateTime.Now, Title = "Stage Software Developer", StartDate = DateTime.Now.AddDays(35), EndDate = DateTime.Now.AddDays(65), DesiredSkills = { new Skill { Id = 7, Name = "Java" }, new Skill { Id = 1, Name = "C#" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink5, ShortDescription = "Wij zijn op zoek naar een geniaal Sotftware Developer" },
new Internship { Id = 6, CompanyId = 5, CreationDate = DateTime.Now, Title = "Stage Sotware Developer", StartDate = DateTime.Now.AddDays(35), EndDate = DateTime.Now.AddDays(65), DesiredSkills = { new Skill { Id = 12, Name = "C++" }, new Skill { Id = 1, Name = "C#" } }, FullTimeOrPartTime = InternshipType.Fulltime, IsRemote = RemoteExpectation.Whatever, PaymentExpectation = PaymentExpectation.Whatever, DescriptionFileUpload = uploadlink6, ShortDescription = "Wij zijn op zoek naar een bliljant Software Developer" }
);
}
public void配置(EntityTypeBuilder)
{
builder.HasData(
新实习{Id=1,CompanyId=1,CreationDate=DateTime.Now,Title=“Stage Software Developer”,StartDate=DateTime.Now.AddDays(30),EndDate=DateTime.Now.AddDays(60),DesiredSkills={new Skill{Id=1,Name=“C#”},新技能{Id=2,Name=“SQL”},FullTimeOrPartTime=internicshipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionFileUploadLink1,ShortDescription=“Wij zijn op zoek naar een schitterende Software Developer”,
新实习{Id=2,CompanyId=2,CreationDate=DateTime.Now,Title=“Stage Android Developer”,StartDate=DateTime.Now.AddDays(32),EndDate=DateTime.Now.AddDays(62),DesiredSkills={new Skill{Id=1,Name=“C#”},新技能{Id=10,Name=“Xamarin”},FullTimeOrPartTime=internicshipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionFileUploadLink2,ShortDescription=“Wij zijn op zoek naar een schitterende Android开发者”,
新实习{Id=3,CompanyId=3,CreationDate=DateTime.Now,Title=“Stage Angular Developer”,StartDate=DateTime.Now.AddDays(35),EndDate=DateTime.Now.AddDays(65),DesiredSkills={new Skill{Id=1,Name=“C#”},新技能{Id=4,Name=“Angular”},FullTimeOrPartTime=internishipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionIleupload=uploadlink3,ShortDescription=“Wij zijn op zoek naar een schitterende Angular Developer”,
新实习{Id=4,CompanyId=3,CreationDate=DateTime.Now,Title=“Stage Java Developer”,StartDate=DateTime.Now.AddDays(35),EndDate=DateTime.Now.AddDays(65),DesiredSkills={new Skill{Id=7,Name=“Java”},new Skill{Id=2,Name=“SQL”},FullTimeOrPartTime=internicshipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionFileUploadLink4,ShortDescription=“Wij zijn op zoek naar een schitterende Java Developer”,
新实习{Id=5,CompanyId=4,CreationDate=DateTime.Now,Title=“Stage Software Developer”,StartDate=DateTime.Now.AddDays(35),EndDate=DateTime.Now.AddDays(65),DesiredSkills={new Skill{Id=7,Name=“Java”},new Skill{Id=1,Name=“C#“},FullTimeOrPartTime=internishipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionIleupload=uploadlink5,ShortDescription=“Wij zijn op zoek naar een geniaal software Developer”,
新实习{Id=6,CompanyId=5,CreationDate=DateTime.Now,Title=“Stage Sotware Developer”,StartDate=DateTime.Now.AddDays(35),EndDate=DateTime.Now.AddDays(65),DesiredSkills={new Skill{Id=12,Name=“C++”},new Skill{Id=1,Name=“C#“},FullTimeOrPartTime=internicshipType.Fulltime,IsRemote=remoteexpection.which,paymentexpection=paymentexpection.which,descriptionIleupload=uploadlink6,ShortDescription=“Wij zijn op zoek naar een bliljant Software Developer”}
);
}
DesiredSkills必须在他们自己的配置中播种。不能为嵌套对象设定种子。