User954137989 posted
Honestly, this should be straight forward but for some reason, this is not working. I am trying to mimic the customization that is done when you convert the ID of a User/Role from the GUIDs(string) to an INT. The difference is, I still want to
use GUIDs, but I want SQL to store them as UniqueIdentifiers and NOT strings.
I have already posted to
StackOverflow (with no responses) and have crawled the web looking for any help.
I am getting these errors when trying to add a new migration:
PeoplesParty.DataAccessLayer.DbContext.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PeoplesParty.DataAccessLayer.DbContext.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined.
Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
// MODELS
[Table("Users", Schema = "User")]
public class ApplicationUser : IdentityUser<Guid,ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUser()
: base()
{
}
}
[Table("Roles", Schema = "Application")]
public class ApplicationRole : IdentityRole<Guid, ApplicationUserRole>
{
public ApplicationRole()
{
Id = Guid.NewGuid();
}
public ApplicationRole(string name)
{
Name = name;
Id = Guid.NewGuid();
}
}
[Table("UserClaims", Schema = "User")]
public class ApplicationUserClaim: IdentityUserClaim<Guid>
{
public ApplicationUserClaim()
: base()
{
}
}
[Table("UserLogins", Schema = "User")]
public class ApplicationUserLogin: IdentityUserLogin<Guid>
{
public ApplicationUserLogin()
: base()
{
}
}
[Table("UserRoles", Schema = "User")]
public class ApplicationUserRole : IdentityUserRole<Guid>
{
public ApplicationUserRole()
: base()
{
}
}
// DB CONTEXT
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(null);
}
public ApplicationDbContext()
: this("PeoplesPartyDB2")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
modelBuilder.HasDefaultSchema("Application");
base.OnModelCreating(modelBuilder);
SetupCustomIdentityTables(modelBuilder);
}
private void SetupCustomIdentityTables(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException(nameof(modelBuilder));
}
// Needed to ensure subclasses share the same table
var user = modelBuilder.Entity<ApplicationUser>().HasKey(au => au.Id).ToTable("Users", "User");
user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId);
user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId);
user.Property(u => u.UserName)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") {IsUnique = true}));
// CONSIDER: u.Email is Required if set on options?
user.Property(u => u.Email).HasMaxLength(256);
modelBuilder.Entity<ApplicationUserRole>().HasKey(ur => new {ur.UserId, ur.RoleId}).ToTable("UserRoles", "User");
modelBuilder.Entity<ApplicationUserLogin>()
.HasKey(ul => new {ul.LoginProvider, ul.ProviderKey, ul.UserId})
.ToTable("UserLogins", "User");
modelBuilder.Entity<ApplicationUserClaim>().HasKey(uc => uc.Id).ToTable("UserClaims", "User");
var role = modelBuilder.Entity<ApplicationRole>().HasKey(ar => ar.Id).ToTable("Roles", "Application");
role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);
role.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") {IsUnique = true}));
}
}