简体   繁体   English

INSERT语句与FOREIGN KEY约束错误冲突

[英]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". INSERT语句与FOREIGN KEY约束“FK_dbo.AspNetUsers_dbo.Contacts_ContactID”冲突。

The conflict occurred in database "aspnet-COGMakati-20140119015553", table "dbo.Contacts", column 'ContactID'. 冲突发生在数据库“aspnet-COGMakati-20140119015553”,表“dbo.Contacts”,列'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 :| 我正在使用Entity Framework和MVC 5的IdentityUser,所以我真的迷失了我正在做的事情:

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. 我看到它的方式, Contact正在创建用户之前创建。 I don't know why this happens, since when not using IdentityUser this code populates it just fine. 我不知道为什么会发生这种情况,因为当不使用IdentityUser这段代码就可以很好地填充它。

Now this is the Register method: 现在这是Register方法:

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);
}

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. 转到您的dbo.Contacts表并使用该键查找记录。 You'll find no record with that key in the table. 您将在表格中找不到包含该密钥的记录。 You need to create a record with that key or use another (existent) key. 您需要使用该密钥创建记录或使用另一个(现有)密钥。

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. 如果您正在创建FK对象,那么您应首先存储它并检索其ID,因此如果没有FK值,则会导致FK_Key失败。

Walter 沃尔特

如果您希望可以允许引用不存在的记录

Sql("ALTER TABLE dbo.Users NOCHECK CONSTRAINT [FK_dbo.Users_dbo.Roles_RoleID]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM