Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

The INSERT statement conflicted with the FOREIGN KEY #3989


User avatar
0
bilalhaidar created

Hi, For the ASP.NET Core / Angular project (v 4.3.0)

I have an entity that defines 2 properties of type User.

public virtual User RcmMember { get; set; }
 public virtual long RcmMemberId { get; set; }

public virtual User RcmCoordinator { get; set; }
public virtual long RcmCoordinatorId { get; set; }

In the DbContext, I have the following:

modelBuilder.Entity<Assignment>().ToTable("Assignments").HasIndex(r => new { r.ReferralId, r.Id }).IsUnique();
            modelBuilder.Entity<Assignment>().Property(p => p.AssignmentDate).IsRequired();
            modelBuilder.Entity<Assignment>().Property(p => p.TransportationId).IsRequired();
            modelBuilder.Entity<Assignment>().Property(p => p.ServiceId).IsRequired();
            modelBuilder.Entity<Assignment>().Property(p => p.State).IsRequired();
            modelBuilder.Entity<Assignment>().Property(p => p.Comments).HasMaxLength(550);
            modelBuilder.Entity<Assignment>().Property(p => p.RcmMemberComments).HasMaxLength(550);
            modelBuilder.Entity<Assignment>().Property(p => p.RcmCoordinatorComments).HasMaxLength(550);

            modelBuilder.Entity<Assignment>()
                .HasOne(e => e.RcmCoordinator)
                .WithMany()
                .HasForeignKey(e => e.RcmCoordinatorId);

            modelBuilder.Entity<Assignment>()
                .HasOne(e => e.RcmMember)
                .WithMany()
                .HasForeignKey(e => e.RcmMemberId);

When I receive the Dto from the client, the RcmMemberId has a value for a valid User Id (the logged-in user would have selected a valid user created in the application from a dropdown list).

Upon running this line of code, I get an exception:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Assignments_AbpUsers_RcmMemberId". The conflict occurred in database "AppDb1", table "dbo.AbpUsers", column 'Id'.
The statement has been terminated.

The code that is causing the exception:

[UnitOfWork]
        public async Task CreateAsync(UserIdentifier user, Assignment input)
        {
            // Assign an Rcm Coordinator
            input.RcmCoordinatorId = user.UserId;

            // Insert the record
            await _repo.InsertAsync(input);

            try
            {
                await CurrentUnitOfWork.SaveChangesAsync();  
            }
            catch (Exception ex)
            {
                var s = ex.ToString();
            }
            [UnitOfWork]
        public async Task CreateAsync(UserIdentifier user, Assignment input)
        {
            // check if there is any collision 
            // TODO
            //CheckCollision(input);

            // Assign an Rcm Coordinator
            input.RcmCoordinatorId = user.UserId;

            // Insert the record
            await _repo.InsertAsync(input);

            try
            {
                await CurrentUnitOfWork.SaveChangesAsync();  
            }
            catch (Exception ex)
            {
                var s = ex.ToString();
            }
            
            // Trigger the Workflow
            await AssignToMember(input);
        }
        }

6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    RcmMemberId defaults to 0, which is not a valid User.Id - set the property type to long? if it's optional.

  • User Avatar
    0
    bilalhaidar created

    Thanks @aaron for your feedback.

    But the RcmMemberId is always filled with a valid value coming from the client. Isn't that what you mean?

    Thanks

  • User Avatar
    0
    aaron created
    Support Team

    Is the client passing both RcmMemberId and RcmCoordinatorId? Set a breakpoint and check both values.

  • User Avatar
    0
    bilalhaidar created

    If you notice in my code that I shared, I am setting myself the RcmCoordinatorId. The other one I get from the client. So yes, I am debugging and I am seeing both ID's with values for existing users in the system.

    Thanks

  • User Avatar
    0
    aaron created
    Support Team

    Your database is saying otherwise. Can you:

    • show some screenshots of the value while debugging and database records, and
    • try hardcoding input.RcmMemberId = 1 or input.RcmMemberId = user.UserId?
  • User Avatar
    0
    bilalhaidar created

    Hello @aaron,

    I figured out the problem. I had a leftover record with an ID that is not present anymore in the system. Hence, the exception. Thanks for your time.