The INSERT statement conflicted with the FOREIGN KEY constraint error

31,987

Solution 1

You are probably trying to access or insert using a non-existent foreign key. Go to your dbo.Contacts Table and look for the record with that key. You'll find no record with that key in the table. You need to create a record with that key or use another (existing) key.

Solution 2

If you are creating the FK obect as well than you should first store it and retrieve its ID, failing todo so will cause the FK_Key to fail as you have no FK value.

Walter

Solution 3

If you want you can allow to reference to non-existent records

Sql("ALTER TABLE dbo.Users NOCHECK CONSTRAINT [FK_dbo.Users_dbo.Roles_RoleID]");
Share:
31,987
Dominic
Author by

Dominic

Updated on July 25, 2022

Comments

  • Dominic
    Dominic almost 2 years

    Hi I'm getting this error

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.Contacts_ContactID".

    The conflict occurred in database "aspnet-COGMakati-20140119015553", table "dbo.Contacts", column 'ContactID'.

    The statement has been terminated.

    I'm using Entity Framework and MVC 5's IdentityUser so I'm really lost on what I'm doing :|

    This is what I'm trying to populate:

    public class User : IdentityUser
    {
        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
    
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
    
        [Required]
        public string Email { get; set; }
    
        public string Birthday { get; set; }
        [Display(Name = "Referred By")]
        public string LifegroupPreference { get; set; }
        [Display(Name = "Civil Status")]
        public string CivilStatus { get; set; }
    
        public int EducationID { get; set; }
        public int WorkID { get; set; }
        public int ContactID { get; set; }
        public int FamilyID { get; set; }
        public int SpiritualID { get; set; }
        public int GrowthMilestoneID { get; set; }
            
        [ForeignKey("EducationID")]
        public virtual Education Education { get; set; }
        [ForeignKey("WorkID")]
        public virtual Work Work { get; set; }
        [ForeignKey("ContactID")]
        public virtual Contact Contact { get; set; }
        [ForeignKey("FamilyID")]
        public virtual Family Family { get; set; }
        [ForeignKey("SpiritualID")]
        public virtual Spiritual Spiritual { get; set; }
        [ForeignKey("GrowthMilestoneID")]
        public virtual GrowthMilestone GrowthMilestone { get; set; }
    }
    

    The way I see it, the Contact is being created before the User is even made. I don't know why this happens, since when not using IdentityUser this code populates it just fine.

    Now this is the Register method:

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = model.GetUser();
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var idManager = new IdentityManager();
                idManager.AddUserToRole(user.Id, "User");
    
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("ViewAllMembers", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }
    
        // If we got this far, something failed, redisplay form
        return View(model);
    }
    
  • Dominic
    Dominic about 10 years
    Thank you! I resolved this by removing the id's responsible for the navigation properties and instead put navigation properties for each table to user.