如何通过 asp.net 项目中的 Seed 传递 FK 的值
Posted
技术标签:
【中文标题】如何通过 asp.net 项目中的 Seed 传递 FK 的值【英文标题】:How do I pass the value of the FK through the Seed in asp.net project 【发布时间】:2021-12-13 12:16:22 【问题描述】:我使用种子将默认数据输入数据库,但我遇到了一个问题,即FK, 如何毫无问题地将 FK 的值传递给两个表?
运行程序时出现此错误: SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_AspNetUsers_Departments_DepartmentId”冲突。数据库“HRS.WEB1”、表“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$$");
【问题讨论】:
那里是什么关系,员工需要部门密钥吗? 是的,员工需要部门密钥,关系是一对多 好吧,当您为员工播种时,您不会传递 DepartmentId。 这是我面临的问题,当我为员工播种时,我不知道如何传递 DepartmentId !! 【参考方案1】:有多种方法可以做到这一点,我不知道你模型的确切结构,但无论如何日志应该是相同的:
第一 - 硬值:
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$$");
第二 - 返回您添加的部门,然后将其作为参数传递:
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$$");
【讨论】:
第二个答案比较准确,不过需要修改一下,谢谢老哥 我的代码需要修改,而不是你的答案以上是关于如何通过 asp.net 项目中的 Seed 传递 FK 的值的主要内容,如果未能解决你的问题,请参考以下文章
在 Db Initializer 的 Seed 方法中创建 Asp.net Identity 用户
通过组合动作链接和 ASP.Net MVC 5 中的输入将用户键入的输入从视图传递到动作控制器?
如何选择一个 ASP.NET 服务器控件传递保存在 jQuery 中的 JS 变量中的 id?
如何从当前用户检索(Jwt)访问令牌并将其传递给 asp.net 样板(abp)中的移动应用程序?