Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The INSERT statement conflicted with the FOREIGN KEY constraint error

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);
}
like image 990
Dominic Avatar asked Feb 03 '14 02:02

Dominic


3 Answers

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.

like image 112
Tassisto Avatar answered Sep 28 '22 01:09

Tassisto


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

like image 45
Walter Verhoeven Avatar answered Sep 28 '22 02:09

Walter Verhoeven


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]");
like image 28
Aleksey Pavlov Avatar answered Sep 28 '22 03:09

Aleksey Pavlov