Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The final (working) OnModelCreating method builds on the answer provided by Hao Kung (whose answer I have accepted and to whom I have awarded the bounty) to create the Foreign Key references from the IdentityUser/ApplicationUser side rather than attempting to do the same from the other direction.</p> <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var user = modelBuilder.Entity&lt;IdentityUser&gt;().HasKey(u =&gt; u.Id).ToTable("User", "Users"); //Specify our our own table names instead of the defaults user.Property(iu =&gt; iu.Id).HasColumnName("Id"); user.Property(iu =&gt; iu.UserName).HasColumnName("UserName"); user.Property(iu =&gt; iu.Email).HasColumnName("EmailAddress").HasMaxLength(254).IsRequired(); user.Property(iu =&gt; iu.IsConfirmed).HasColumnName("EmailConfirmed"); user.Property(iu =&gt; iu.PasswordHash).HasColumnName("PasswordHash"); user.Property(iu =&gt; iu.SecurityStamp).HasColumnName("SecurityStamp"); user.HasMany(u =&gt; u.Roles).WithRequired().HasForeignKey(ur =&gt; ur.UserId); user.HasMany(u =&gt; u.Claims).WithRequired().HasForeignKey(uc =&gt; uc.UserId); user.HasMany(u =&gt; u.Logins).WithRequired().HasForeignKey(ul =&gt; ul.UserId); user.Property(u =&gt; u.UserName).IsRequired(); var applicationUser = modelBuilder.Entity&lt;ApplicationUser&gt;().HasKey(au =&gt; au.Id).ToTable("User", "Users"); //Specify our our own table names instead of the defaults applicationUser.Property(au =&gt; au.Id).HasColumnName("Id"); applicationUser.Property(au =&gt; au.NumericId).HasColumnName("NumericId"); applicationUser.Property(au =&gt; au.UserName).HasMaxLength(50).HasColumnName("UserName"); applicationUser.Property(au =&gt; au.PasswordHash).HasColumnName("PasswordHash"); applicationUser.Property(au =&gt; au.SecurityStamp).HasColumnName("SecurityStamp"); applicationUser.Property(au =&gt; au.DisplayName).HasColumnName("DisplayName"); applicationUser.Property(au =&gt; au.Description).HasColumnName("Description"); applicationUser.Property(au =&gt; au.Email).HasColumnName("EmailAddress").HasMaxLength(254).IsRequired(); applicationUser.Property(au =&gt; au.IsConfirmed).HasColumnName("EmailConfirmed"); applicationUser.Property(au =&gt; au.Sequence).HasColumnName("Sequence"); applicationUser.Property(au =&gt; au.ExternalRef).HasColumnName("ExternalRef"); applicationUser.Property(au =&gt; au.LoggedOn).HasColumnName("LoggedOn"); applicationUser.Property(au =&gt; au.LoggedOff).HasColumnName("LoggedOff"); applicationUser.Property(au =&gt; au.LastActivity).HasColumnName("LastActivity"); applicationUser.Property(au =&gt; au.FailedLoginAttempts).IsOptional().HasColumnName("FailedLoginAttempts"); applicationUser.Property(au =&gt; au.LockedOutUntil).IsOptional().HasColumnName("LockedOutUntil"); applicationUser.Property(au =&gt; au.LockOutCycles).IsOptional().HasColumnName("LockOutCycles"); applicationUser.Property(au =&gt; au.Approved).HasColumnName("Approved"); var role = modelBuilder.Entity&lt;IdentityRole&gt;().HasKey(ir =&gt; ir.Id).ToTable("ApplicationRole", "Users"); role.Property(ir =&gt; ir.Id).HasColumnName("Id"); role.Property(ir =&gt; ir.Name).HasColumnName("Name"); var claim = modelBuilder.Entity&lt;IdentityUserClaim&gt;().HasKey(iuc =&gt; iuc.Id).ToTable("UserClaim", "Users"); claim.Property(iuc =&gt; iuc.Id).HasColumnName("Id"); claim.Property(iuc =&gt; iuc.ClaimType).HasColumnName("ClaimType"); claim.Property(iuc =&gt; iuc.ClaimValue).HasColumnName("ClaimValue"); claim.Property(iuc =&gt; iuc.UserId).HasColumnName("UserId"); var login = modelBuilder.Entity&lt;IdentityUserLogin&gt;().HasKey(iul =&gt; new { iul.UserId, iul.LoginProvider, iul.ProviderKey }).ToTable("UserLogin", "Users"); //Used for third party OAuth providers login.Property(iul =&gt; iul.UserId).HasColumnName("UserId"); login.Property(iul =&gt; iul.LoginProvider).HasColumnName("LoginProvider"); login.Property(iul =&gt; iul.ProviderKey).HasColumnName("ProviderKey"); var userRole = modelBuilder.Entity&lt;IdentityUserRole&gt;().HasKey(iur =&gt; new { iur.UserId, iur.RoleId }).ToTable("UserRole", "Users"); userRole.Property(ur =&gt; ur.UserId).HasColumnName("UserId"); userRole.Property(ur =&gt; ur.RoleId).HasColumnName("RoleId"); } </code></pre> <p>I also liked the (brilliant yet so simple I can't think why I didn't do it before) idea of referencing the Model Builder Entity as a variable to use on each following fluent-api statement.</p> <p>I referenced properties and foreign keys on both entities, although (in practice) it does not appear that this is required. On ASP.NET Identity 1.0 there was no problem omitting the property mappings, but since I have changed the names of Email and IsConfirmed database columns at least those needed to be mapped against the IdentityUser entity in ASP.NET Identity 1.1</p> <p>Likewise the ApplicationUser class probably does not need to foreign key defined (as relationships are defined between the base class and the other entities) but I saw no harm in ensuring there was no assumption of additional foreign key columns on the other entity tables by adding a explicit definition - and it doesn't seem to have done any harm.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload