Our application doesn't need the excessive "Logins" and "Claims" functionality that identity uses. It would be nice if these tables simlpy didn't get created in the database, but I don't want to have to reimplement all identity classes...
I'd assume it's something like
public ApplicationDbContext : IdentityDbContext
{
[...]
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore<IdentityUserClaim>();
modelBuilder.Ignore<IdentityUserLogin>();
}
}
You would think this works according to the function descriptions, but it doesn't. The AspNetUserClaim
and AspNetUserLogins
tables still get created.
What's the right way to do this?
Here is an implementation of ApplicationDbContext
's OnModelCreating
method for your case. In fact it's just IdentityDbContext
's OnModelCreating
method with ignoring of IdentityUserClaim
and IdentityUserLogin
entities.
Note that OnModelCreating
override should not invoke base.OnModelCreating
method.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Needed to ensure subclasses share the same table
var user = modelBuilder.Entity<ApplicationUser>()
.ToTable("AspNetUsers");
user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
user.Ignore(u => u.Claims);
user.Ignore(u => u.Logins);
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<IdentityUserRole>()
.HasKey(r => new { r.UserId, r.RoleId })
.ToTable("AspNetUserRoles");
var role = modelBuilder.Entity<IdentityRole>()
.ToTable("AspNetRoles");
role.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") { IsUnique = true }));
role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);
modelBuilder.Ignore<IdentityUserLogin>();
modelBuilder.Ignore<IdentityUserClaim>();
}
}
Here are the relevant lines of code:
user.Ignore(u => u.Claims);
user.Ignore(u => u.Logins);
modelBuilder.Ignore<IdentityUserLogin>();
modelBuilder.Ignore<IdentityUserClaim>();
As required, it would result in the following migration without dbo.AspNetUserClaims
and dbo.AspNetUserLogins
tables:
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With