如何通过 asp.net 项目中的种子传递 FK 的值
How do I pass the value of the FK through the Seed in asp.net project
我使用种子将默认数据输入数据库,但我遇到了一个问题,就是 FK,
如何将 FK 的值毫无问题地传递给两个 table?
我在 运行 程序时遇到此错误:
SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_AspNetUsers_Departments_DepartmentId”冲突。冲突发生在数据库“HRS.WEB1”、table“dbo.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$$");
}
}
有多种方法可以做到这一点,我不知道你的模型的确切结构,但日志应该是一样的:
首先 - 硬值:
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$$");
}
其次 - Return 你添加的部门,然后将其作为参数传递:
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$$");
}
我使用种子将默认数据输入数据库,但我遇到了一个问题,就是 FK, 如何将 FK 的值毫无问题地传递给两个 table?
我在 运行 程序时遇到此错误: SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_AspNetUsers_Departments_DepartmentId”冲突。冲突发生在数据库“HRS.WEB1”、table“dbo.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$$");
}
}
有多种方法可以做到这一点,我不知道你的模型的确切结构,但日志应该是一样的:
首先 - 硬值:
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$$");
}
其次 - Return 你添加的部门,然后将其作为参数传递:
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$$");
}