gpt4 book ai didi

c# - Entity Framework 6 : The INSERT statement conflicted with the FOREIGN KEY constraint Error but the parent record has been saved

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:52 25 4
gpt4 key购买 nike

我的数据库中有两个表,UsersJobSeekerEmploymentDetails

Users
---------------------------
ID int (Auto Generated Primary Key)
Col1 ...

JobSeekerEmploymentDetails
---------------------------
ID int (Auto Generated Primary Key)
Col1 ...
UserId int (Foreign Key to Users table)

要在上面的表中插入记录,我的代码如下:(我使用的是EntityFramework 6AutoMapper)

    public async Task<int> SaveJobSeeker(AccountInfoViewModel accountInfo, PersonalDetailViewModel personalDetail, EmploymentDetailsViewModel employmentDetails)
{
//create EF User object
var user = new User {CreatedOn = DateTime.Now};

//map data from the ViewModels into user class
Mapper.Map(accountInfo, user);
Mapper.Map(personalDetail, user);

using (var db = new ITWebEntities())
{
//save user
db.Users.Add(user);
await db.SaveChangesAsync();

//Create new EF class object and set recently saved user Id as foreign key value
var jobSeekerEmpDetail = new JobSeekerEmploymentDetail { UserId = user.ID };

//Map other detail from the ViewModel object
Mapper.Map(employmentDetails, jobSeekerEmpDetail);

//save emp details info
db.JobSeekerEmploymentDetails.Add(jobSeekerEmpDetail);
return await db.SaveChangesAsync();
}
}

现在 User 已保存到数据库中,我可以看到 ID 的值已从保存的 User 对象中成功检索。但是最后一个 db.SaveChangesAsync() 语句抛出以下异常。

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_EmploymentDetails_Users". The conflict occurred in database "ITWeb", table "dbo.Users", column 'ID'.
The statement has been terminated.

如以下屏幕截图所示,约束已正确设置:

enter image description here

我想我正在做一些愚蠢的事情,但我无法弄明白。你能帮忙吗?

最佳答案

这很简单。 Mapper.Map(employmentDetails, jobSeekerEmpDetail); 行实际上会将 UserId 覆盖为我在上一步中正确设置的 0。在执行映射并且它起作用后,我更改了代码以设置 UserId

public async Task<int> SaveJobSeeker(AccountInfoViewModel accountInfo, PersonalDetailViewModel personalDetail, EmploymentDetailsViewModel employmentDetails)
{
//create EF User object
var user = new User {CreatedOn = DateTime.Now};

//map data from the ViewModels into user class
Mapper.Map(accountInfo, user);
Mapper.Map(personalDetail, user);

using (var db = new ITWebEntities())
{
//save user
db.Users.Add(user);
await db.SaveChangesAsync();

//Create new EF class object and set recently saved user Id as foreign key value
var jobSeekerEmpDetail = new JobSeekerEmploymentDetail();

//Map other detail from the ViewModel object
Mapper.Map(employmentDetails, jobSeekerEmpDetail);

jobSeekerEmpDetail.UserId = user.ID;

//save emp details info
db.JobSeekerEmploymentDetails.Add(jobSeekerEmpDetail);
return await db.SaveChangesAsync();
}
}

关于c# - Entity Framework 6 : The INSERT statement conflicted with the FOREIGN KEY constraint Error but the parent record has been saved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com