Adding an Object to an Identity Model

I am currently receiving an error message I understand the error, but I do not know where I am mistaken

The ALTER TABLE statement was against the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.CompanyDetails_userCompanyID". The conflict occurred in the database "PXWHITESPIDERDEV", the table "dbo.CompanyDetails", in the column "company identifier".

Internally, IdentityModel is automatically generated when an MVC application is created.

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 object I'm 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; } } 

and inside the class RegisterViewModel

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

before the company identifier was the same in the ApplicationUser class and in the CompanyDetails class, as was the case with the same variable name. I thought this was a problem, so the variable name in the ApplicationUser class changed until I tried to update the database and found out that it wasn’t.

+1
source share
1 answer

When you added a new companyID property, Entity Framework, obviously you had to add a new column to the dbo.AspNetUsers table to represent it. Since this column did not exist before and is not null, a default value must be set in the column for existing records. For int , the default value is 0 . However, 0 not acceptable as a foreign key, so when the Entity Framework tries to bind this constraint, it fails.

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

Alternatively, you can also make companyID nullable int, which would allow the Entity Framework to nullify it in the database when adding a column. Foreign key constraints can be added to columns with zeros, so then everything should work. However, this means that now the relationship will be optional. If each user always has an associated CompanyDetails , find another way to fix the problem.

+2
source

All Articles