我使用seed将默认数据输入数据库,但我面临一个问题,这是FK,如何将FK的值传递给这两个表而不会出现任何问题?
当我运行程序时,我得到这个错误:SqlException: INSERT语句与外键约束" fk_aspnetusers_departments_departmentd& quot;冲突。冲突发生在数据库"hr . web1"、表"dba . departments"和列"Id"中。语句已终止
public static class DbSeeder
{
public static IHost SeedDb(this IHost webHost)
{
using var scope = webHost.Services.CreateScope();
try
{
var context = scope.ServiceProvider.GetRequiredService<HRSDbContext>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<Employee>>();
context.SeedDepartment().Wait();
userManager.SeedEmployee().Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
return webHost;
}
public static async Task SeedDepartment(this HRSDbContext _db)
{
if (await _db.Departments.AnyAsync())
{
return;
}
var departments = new List<Department>();
var department = new Department();
department.Name = "A1";
department.Id = 1;
department.CreatedAt = DateTime.Now;
var department2 = new Department();
department2.Name = "A2";
department2.Id = 2;
department2.CreatedAt = DateTime.Now;
departments.Add(department);
departments.Add(department2);
await _db.Departments.AddRangeAsync(departments);
await _db.SaveChangesAsync();
}
public static async Task SeedEmployee(this UserManager<Employee> userManger)
{
if (await userManger.Users.AnyAsync())
{
return;
}
var user = new Employee();
user.FullName = "System Developer";
user.UserName = "dev@gmail.com";
user.Email = "dev@gmail.com";
user.CreatedAt = DateTime.Now;
await userManger.CreateAsync(user, "Admin111$$");
}
}
有多种方法可以做到这一点,我不知道你的模型的确切结构,但日志应该是相同的,无论:
First - Hard value in:
public static async Task SeedEmployee(this UserManager<Employee> userManger)
{
if (await userManger.Users.AnyAsync())
{
return;
}
var user = new Employee();
user.FullName = "System Developer";
user.UserName = "dev@gmail.com";
user.Email = "dev@gmail.com";
user.CreatedAt = DateTime.Now;
user.DepartmentId = 1; //here
await userManger.CreateAsync(user, "Admin111$$");
}
Second -返回添加的部门,然后将其作为参数传递:
public static IHost SeedDb(this IHost webHost)
{
using var scope = webHost.Services.CreateScope();
try
{
var context = scope.ServiceProvider.GetRequiredService<HRSDbContext>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<Employee>>();
var departments = context.SeedDepartment().Wait();
userManager.SeedEmployee(departments.First().Id ).Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
return webHost;
}
public static async Task<IEnumerable<Department>> SeedDepartment(this HRSDbContext _db)
{
var _departments = await _db.Departments.AllAsync()
if (_departments != null)
{
return _departments;
}
var departments = new List<Department>();
var department = new Department();
department.Name = "A1";
department.Id = 1;
department.CreatedAt = DateTime.Now;
var department2 = new Department();
department2.Name = "A2";
department2.Id = 2;
department2.CreatedAt = DateTime.Now;
departments.Add(department);
departments.Add(department2);
await _db.Departments.AddRangeAsync(departments);
await _db.SaveChangesAsync();
return departments;
}
public static async Task SeedEmployee(this UserManager<Employee> userManger, int departmentId)
{
if (await userManger.Users.AnyAsync())
{
return;
}
var user = new Employee();
user.FullName = "System Developer";
user.UserName = "dev@gmail.com";
user.Email = "dev@gmail.com";
user.CreatedAt = DateTime.Now;
user.DepartmentId = departmentId;
await userManger.CreateAsync(user, "Admin111$$");
}