[英]Better way to Seed a Dot Net Core Entity Framework Database?
I'm working to seed a database I'm creating for a United Way I'm volunteering for. 我正在为我正在志愿服务的联合之路创建的数据库播种。 (Check out TapRoot+ if you are interested in volunteering and applying some learning)
(如果您对志愿服务和应用学习感兴趣,请查看TapRoot +)
Anyways, I'm only seeding one to many fields right now, but I've gotten it to work one table at a time. 无论如何,我现在只播种一对多的字段,但是我已经使它一次只能处理一张表。
public class Seed
{
private CharEmContext _context;
public Seed(CharEmContext context)
{
_context = context;
}
public async Task SeedCounty()
{
if (!_context.Counties.Any())
{
_context.AddRange(_counties);
await _context.SaveChangesAsync();
}
}...`
and 和
static List<County> _counties = new List<County>
{
new County { Name = "Emmet"},
new County { Name = "Charlevoix"},
new County { Name = "Antrim"},
new County { Name = "Cheboygan"},
new County { Name = "Otsego"},
new County { Name = "None"}
};
But run into trouble when I'm trying to reference the Id's assigned once they are created. 但是,当我尝试引用创建的ID时遇到麻烦。
Neither this: 这都不是:
static List<City> _cities = new List<City>
{
new City { Name = "Alanson", CountyId = _context.Counties.Where(x=>x.Name =="Emmet").Select(x => x.Id).FirstOrDefault()}, ... }
Nor this: 也没有:
static List<City> _cities = new List<City>
{
new City { Name = "Alanson", CountyId = _counties.Where(x=>x.Name =="Emmet").Select(x => x.Id).FirstOrDefault()},
works. 作品。
For reference I'm creating a service in my startup.cs.ConfigureServices
供参考,我在
startup.cs.ConfigureServices
创建服务
services.AddTransient(); services.AddTransient();
and calling each add method in the .Configure(Seed Seeder)
并在
.Configure(Seed Seeder)
调用每个add方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, Seed seeder)
{
seeder.SeedCounty().Wait();
seeder.SeedCity().Wait();
seeder.SeedLocation().Wait();
Definitely need to know how to reference those IDs the DB creates on the dependent tables. 绝对需要知道如何引用数据库在从属表上创建的那些ID。
Also curious if I need to seed the linking tables in the many-to-many relationships... 也好奇我是否需要在多对多关系中播种链接表...
So the key was to create explicit dictionaries: 因此,关键是创建显式词典:
Dictionary<string, int> County;
Dictionary<string, int> City;
Dictionary<string, int> Address;
Dictionary<string, int> ServiceType;
Dictionary<string, int> Organization;
Dictionary<string, int> Contact;`
and populate them within the seed actions: 并在种子操作中填充它们:
public async Task SeedCounty()
{
if (!_context.Counties.Any())
{
_context.AddRange(_counties);
await _context.SaveChangesAsync();
}
County = _context.Counties.ToDictionary(p => p.Name, p => p.Id);
}
Then to create subsequent lists that reference the ID's that are now seeded, a separate call is made to create each dependent list to be seeded: 然后,要创建引用现在已播种的ID的后续列表,请进行单独的调用以创建要播种的每个从属列表:
public async Task SeedCity()
{
CreateCities(); // Create Dependent List
if (!_context.Cities.Any())
{
_context.AddRange(_cities); //Seed List
await _context.SaveChangesAsync();
}
City = _context.Cities.ToDictionary(p => p.Name, p => p.Id); //Create Dictionary with New Ids
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.