The INSERT statement conflicted with the FOREIGN KEY constraint "FK_StudyClasses_AspNetUsers_AppUserId".
Hi so im making lets say table 1 it will have a foregin key from table 2 now the table 2 is an asp.net identity user and has an id that is created by entity framework i want to use the asp net user id as a foriegn key but when i do and try to insert data through lets say swagger or postman i get the above error of insert statment conflicted ive been using this foreign key system through out my application but it is this asp net user table that gives an error
ASP.NET Core
ASP.NET
-
AgaveJoe 25,866 Reputation points
2022-11-12T11:16:01.447+00:00 The screen shot shows the AppUser class is missing an ending bracket. Therefore the code you've shared is not the code that caused the error!
Anyway, the error indicates the code tries to insert a StudyClass where the AppUserId does not exist in AppUser. What is the value of AppUserId in StudyClass? Does the code actually assing a AppUserId? Use the Visual Studio debugger to figure out what's happening in the code.
Unfortunately, you did not share all the relevant code so we cannot point out the bug(s) in the code base.
-
Asjad Butt 66 Reputation points
2022-11-15T06:53:39.827+00:00 the app user id is auto generated by identity services when i cosole.log the user i get an id also an id is visible in the sql table is it necessary to add the id in the entity table to accesss it
-
Zhi Lv - MSFT 31,946 Reputation points • Microsoft Vendor
2022-11-17T06:10:06.387+00:00 Hi @Asjad Butt ,
As AgaveJoe said, this issue happens when you try to insert a new StudentClass (you have set the AppUser property), but the new StudentClass.AppUser.AppUserId is not exist in the AspNetUsers table, so you need to check this value from the new StudentClass and the AspNetUsers table.
If you will create a new user when insert the new StudentClass, try to insert the user first, after that get the exist user and use it to insert the new StudentClass.
-
Asjad Butt 66 Reputation points
2022-11-17T06:19:54.373+00:00 sir i am doing exactly the same the user is already registered it acts a teacher for the class the user is already registered i just use the user id as a foreign key reference when adding the studyclass but it gives this error as i said i have use this funtionality many times across my application but its only the appuser when used as a foreign class gives this error
-
Zhi Lv - MSFT 31,946 Reputation points • Microsoft Vendor
2022-11-17T09:19:18.15+00:00 Ok, I got it. Can you share the AddStudentClass() method code? And it is better to create a minimal, complete sample to reproduce the problem, and share it via Github.
-
Asjad Butt 66 Reputation points
2022-11-17T12:42:03.513+00:00 letme know if u need anything else
-
AgaveJoe 25,866 Reputation points
2022-11-17T13:38:30.837+00:00 For the second time, the code you shared does not compile and therefore cannot be the actual code that is executing. We need the actual code and please do not post screenshots. Share code the community can actually run.
Keep in mind, the StudentClassDto does not have an AppUserId property. I assume the mapping does not set this AppUserId which causes the foreign key constraint error. Please run your code through the Visual Studio debugger. Set a break point and view the property values in studyClass var after the mapper runs.
-
Asjad Butt 66 Reputation points
2022-11-18T13:18:01.01+00:00 **
- //IDENTTY USER (App User)
**
public class AppUser : IdentityUser<int>
{
public override int Id { get; set; }
public override string UserName { get; set; }
public string? FatherName { get; set; }
public int? BranchId { get; set; }
public Branch? Branch { get; set; }
public ICollection<AppUserRole> UserRoles { get; set; }
}
**- //StudyClass
**
public class StudyClass { public int Id { get; set; } public string ClassName { get; set; } public DateTime ClassTime { get; set; } public int AppUserId { get; set; } public AppUser? AppUser { get; set; } }
**
- //StudyClass Dto
**
public class StudyClassDto { public int Id { get; set; } public string ClassName { get; set; } public DateTime ClassTime { get; set; } public int TeacherId { get; set; } }
**
- //Add StudyClass Controller method
**
[HttpPost] public async Task<IActionResult> AddClass(StudyClassDto studyClassDto) { var studyclass = mapper.Map<StudyClass>(studyClassDto); uow.StudyClassRepository.AddStudyClass(studyclass); await uow.SaveAsync(); return Ok(); }
-
Asjad Butt 66 Reputation points
2022-11-18T13:18:33.917+00:00 **
- //Maping Profile
**
CreateMap<StudyClassDto, StudyClass>() .ReverseMap() .ForMember(dest => dest.TeacherId, opt => opt.MapFrom(src => src.AppUserId));
-
AgaveJoe 25,866 Reputation points
2022-11-18T13:44:54.663+00:00 Please debug your code!
Set a break point and find the value of TeacherId before and after the mapping! Look up the value in the database to see if the value exists. If TeacherId is zero, this could point to an issue with the client.
Sign in to comment