I am using EF Core 5 and have extended EF Core Identity to manage user auth. But when I migrate my Entity objects to the database it creates extra columns in the database for example:
AspNetUserRole
table creates 4 columns in the database UserId
UserId1
RoleId
RoleId1
in the database.
Here are my extended classes to give you more insights:
ASP.NET USER ENTITY:
public class AspNetUser : IdentityUser
{
public AspNetUser()
{
UserRoles = new List<AspNetUserRoles>();
AspNetUserShippingInfo = new List<AspNetUserShippingInfo>();
AspNetUserPaymentInfo = new List<AspNetUserPaymentInfo>();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string ExternalUserId { get; set; }
public string ExternalProviderName { get; set; }
public bool VerifiedEmail { get; set; }
[ForeignKey("Vendor")]
public int? VendorId { get; set; }
public virtual Vendor Vendor { get; set; }
//TODO: remove from here. doesn't belong here maybe
[ForeignKey("DeliveryCompany")]
public int? DeliveryCompanyId { get; set; }
public virtual DeliveryCompanies DeliveryCompany { get; set; }
public virtual IList<AspNetUserShippingInfo> AspNetUserShippingInfo { get; set; }
public virtual IList<AspNetUserPaymentInfo> AspNetUserPaymentInfo { get; set; }
public virtual IList<AspNetUserRoles> UserRoles { get; set; }
public int? PreferredZipCode { get; set; }
public string PreferredLanuage { get; set; }
}
ASP.NET ROLE ENTITY:
public class AspNetRoles : IdentityRole<string>
{
public AspNetRoles()
{
UserRoles = new List<AspNetUserRoles>();
}
public virtual IList<AspNetUserRoles> UserRoles { get; set; }
}
ASP.NET USER ROLE ENTITY:
public class AspNetUserRoles : IdentityUserRole<string>
{
public AspNetUserRoles() : base()
{
}
public override string UserId { get; set; }
[ForeignKey("UserId")]
public virtual AspNetUser User { get; set; }
public override string RoleId { get; set; }
[ForeignKey("RoleId")]
public virtual AspNetRoles Role { get; set; }
}
I'm not sure what I'm doing wrong that generates these extra columns in the database. any help will be highly appreciated. Thanks
You can do the following step to solve your issue.
1:change your AspNetUserRoles
public class AspNetUserRoles : IdentityUserRole<string>
{
public AspNetUserRoles() : base()
{
}
public override string UserId { get; set; }
public virtual AspNetUser User { get; set; }
public override string RoleId { get; set; }
public virtual AspNetRoles Role { get; set; }
}
2:In your context:
public class ApplicationDbContext : IdentityDbContext<AspNetUser, AspNetRoles, string,
IdentityUserClaim<string>, AspNetUserRoles, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//add this code,do not set a composite primary key.
modelBuilder.Entity<AspNetUser>(b =>
{
b.HasMany(e => e.UserRoles)
.WithOne(e => e.User)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
modelBuilder.Entity<AspNetRoles>(b =>
{
b.HasMany(e => e.UserRoles)
.WithOne(e => e.Role)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
});
}
3:Remigration and update database.