I
I-NOZex
Guest
I have the exact problem described here: Register custom UserProfile in ASP.NET MVC4 results in duplicate tables (stackoverflow.com/questions/14836085/register-custom-userprofile-in-asp-net-mvc4-results-in-duplicate-tables#comment20793410_14836085)
But the solution doesnt work for me, because its for MVC4 and i use the MVC5.
The problem, is when i try to register a new user, duplicates almost all tables in database for a plural name, eg: Picture (duplicate to) Pictures
I dont know what i should do, and why this occurs, but please be free to ask for more information. I do not know what to say more for now, but I will edit this post if you need.
Thanks in advance, its urgent.
Edit:
I have an extended class from IdentityUser, named ApplicationUser, to put in this model additional attributes, like the id of postal code, photo and name:
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 string Name { get; set; }
public string Photo { get; set; }
public int PostalCodeID { get; set; }
[ForeignKey("PostalCodeID")]
public virtual PostalCode PostalCode { get; set; }
public virtual ICollection<Auction> Auctions { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<Message> Messages { get; set; }
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<BlockHistory> Blocks { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
public DbSet<PostalCode> PostalCode { get; set; }
public ApplicationDbContext()
// ConnectionString
: base("AuctionsContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
The PostalCodeID is a foreign key for the table/model "PostalCode". One user can have one postalcode, but one postalcode could have zero or several users.
Here is the model "PostalCode":
public class PostalCode {
public int ID { get; set; }
[RegularExpression(@"\d{4}-\d{3}", ErrorMessageResourceType=typeof(Resources), ErrorMessageResourceName="PostalCodeFormat")]
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Required")]
[StringLength(8, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Length")]
[Display(Name = "Name_PostalCode", ResourceType = typeof(Resources))]
public string ZipCode { get; set; }
public int LocalityID { get; set; }
public virtual Locality Locality { get; set; }
}
I have the migrations enabled, but not in automatic mode,I think this is happening because in the configuration.cs, i have this code public Configuration() { AutomaticMigrationsEnabled = false; }
When i create the first migration, and updated the database, all tables were correctly created (with their names in singular), except the AspNetxxxx tables, which are created automatically when the first user registration occurs, and that is when most tables are duplicated, and the weblogger gives this error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column ID.
The statement has been terminated.
System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column ID.
The statement has been terminated.
Linha 164: PostalCodeID = model.PostalCodeID
Linha 165: };
Linha 166: var result = await UserManager.CreateAsync(user, model.Password);
Linha 167: if (result.Succeeded)
Linha 168: {
I understand what is happening which leads to this error, in some way, the IdentityUser cant find the PostalCode table, so, it creates the PostalCodes table, and its on this table where its created the relationship between the PostalCodes and AspNetUsers (Ive checked, and there is no relation between tables AspNetUsers and PostalCode)
PS: Ive tried to put [Table("PostalCode")] before public class PostalCode, but when registering a user I got an error saying dbo.AspNetUsers is an invalid object so I verified the database and noticed that it wasnt creating those automatic AspNetxxxx tables.
Sorry for any mistake.
Continue reading...
But the solution doesnt work for me, because its for MVC4 and i use the MVC5.
The problem, is when i try to register a new user, duplicates almost all tables in database for a plural name, eg: Picture (duplicate to) Pictures
I dont know what i should do, and why this occurs, but please be free to ask for more information. I do not know what to say more for now, but I will edit this post if you need.
Thanks in advance, its urgent.
Edit:
I have an extended class from IdentityUser, named ApplicationUser, to put in this model additional attributes, like the id of postal code, photo and name:
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 string Name { get; set; }
public string Photo { get; set; }
public int PostalCodeID { get; set; }
[ForeignKey("PostalCodeID")]
public virtual PostalCode PostalCode { get; set; }
public virtual ICollection<Auction> Auctions { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<Message> Messages { get; set; }
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<BlockHistory> Blocks { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
public DbSet<PostalCode> PostalCode { get; set; }
public ApplicationDbContext()
// ConnectionString
: base("AuctionsContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
The PostalCodeID is a foreign key for the table/model "PostalCode". One user can have one postalcode, but one postalcode could have zero or several users.
Here is the model "PostalCode":
public class PostalCode {
public int ID { get; set; }
[RegularExpression(@"\d{4}-\d{3}", ErrorMessageResourceType=typeof(Resources), ErrorMessageResourceName="PostalCodeFormat")]
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Required")]
[StringLength(8, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Length")]
[Display(Name = "Name_PostalCode", ResourceType = typeof(Resources))]
public string ZipCode { get; set; }
public int LocalityID { get; set; }
public virtual Locality Locality { get; set; }
}
I have the migrations enabled, but not in automatic mode,I think this is happening because in the configuration.cs, i have this code public Configuration() { AutomaticMigrationsEnabled = false; }
When i create the first migration, and updated the database, all tables were correctly created (with their names in singular), except the AspNetxxxx tables, which are created automatically when the first user registration occurs, and that is when most tables are duplicated, and the weblogger gives this error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column ID.
The statement has been terminated.
System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.PostalCodes_PostalCodeID". The conflict occurred in database "PSIProject", table "dbo.PostalCodes", column ID.
The statement has been terminated.
Linha 164: PostalCodeID = model.PostalCodeID
Linha 165: };
Linha 166: var result = await UserManager.CreateAsync(user, model.Password);
Linha 167: if (result.Succeeded)
Linha 168: {
I understand what is happening which leads to this error, in some way, the IdentityUser cant find the PostalCode table, so, it creates the PostalCodes table, and its on this table where its created the relationship between the PostalCodes and AspNetUsers (Ive checked, and there is no relation between tables AspNetUsers and PostalCode)
PS: Ive tried to put [Table("PostalCode")] before public class PostalCode, but when registering a user I got an error saying dbo.AspNetUsers is an invalid object so I verified the database and noticed that it wasnt creating those automatic AspNetxxxx tables.
Sorry for any mistake.
Continue reading...