添加实体身份模型 [英] Adding entity to Identity model

查看:145
本文介绍了添加实体身份模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前得到一个错误我理解的错误,但我不知道我错了。

I am currently getting an error I understand the error but I don't know where I am going wrong

ALTER TABLE语句几乎与冲突外键约束FK_dbo.AspNetUsers_dbo.CompanyDetails_userCompanyID。冲突发生于数据库PXWHITESPIDERDEV,表dbo.CompanyDetails,列companyID。

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.CompanyDetails_userCompanyID". The conflict occurred in database "PXWHITESPIDERDEV", table "dbo.CompanyDetails", column 'companyID'.

在IdentityModel自动生成的,当你创建一个MVC应用程序

Within the IdentityModel autogenerated when you create an MVC application

public class ApplicationUser : IdentityUser
{

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }


    public int userCompanyID { get; set; }

    [ForeignKey("userCompanyID")]
    public CompanyDetails company { get; set; }
}

和这里是我试图创建实体

and here is the entity I am trying to create

 public class CompanyDetails
 {
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int companyID { get; set; }

    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }
 }

和RegisterViewModel类中

and within the RegisterViewModel class

public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public CompanyDetails company { get; set; }
}

前companyID的分别的ApplicationUser类和CompanyDetails类中一样在他们有相同的变量名。我认为这是问题的ApplicationUser类中这么改的变量名,直到我试图再次更新数据库,并发现了这个情况并非如此。

before the companyID's were the same within the ApplicationUser class and the CompanyDetails class as in they had the same variable name. I thought this was the problem so changed the variable name within the ApplicationUser class, until I tried to update the database again and found out this was not the case.

推荐答案

当您添加新的 companyID 属性,实体框架显然需要一个新列添加到 dbo.AspNetUsers 表重新present它。由于本专栏之前并不存在,非空的,需要一些默认值在现有记录的列设置。对于 INT 类型,默认值为 0 。然而, 0 是不可接受的作为外键,所以,当实体框架试图附上约束,它失败。

When you added the new companyID property, Entity Framework obviously needed to add a new column to the dbo.AspNetUsers table to represent it. Since this column did not exist before and is non-nullable, some default value needed to be set in the column for existing records. For an int type, the default value is 0. However, 0 is unacceptable as a foreign key, so when Entity Framework attempts to attach that constraint, it fails.

要解决这个问题最简单的方法是1)从 dbo.AspNetUsers 删除所有现有行或2)添加列并手动更新值实际连接外键约束之前CompanyDetails 的ID。

The easiest way to solve the problem is either 1) remove all existing rows from dbo.AspNetUsers or 2) add the column and manually update the values to actual CompanyDetails ids before attaching the foreign key constraint.

另外,你也可以让 companyId 可空的整型,这将使​​实体框架来将其在数据库中添加列时。外键约束可以被添加到柱空值,所以一切都应该工作,然后。然而,这意味着该关系现在将是可选的。如果每个用户都应该始终有一个相关的 CompanyDetails ,然后找到另一种方式来解决这个问题。

Alternatively, you can also make companyId a nullable int, which would allow Entity Framework to null it out in the database when adding the column. Foreign key constraints can be added to columns with nulls, so everything should work then. However, that means the relationship will now be optional. If every user should always have a related CompanyDetails, then find another way to fix the issue.

这篇关于添加实体身份模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆