2.2: Identity Models Setup
Identity Model
In the ElevenNote.Data assembly, open the
IdentityModels.csfile. Please note that we will be adding a lot of code in the next few steps. (2.2a-IdentityModelsScreenshot.md) Refer to this screenshot to see it all in context:
You will have an error on <Note> until the beginning of part 3, don't worry about it for now.
Look for the
ApplicationDbContextfunction.Underneath the function, but still inside the namespace, create the following
IdentityUserLoginConfigurationclass and method. This code will go right above the closing curly brace for the file:
public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
{
public IdentityUserLoginConfiguration()
{
HasKey(iul => iul.UserId);
}
}
} //<----Closing curly brace of fileCTRL .to bring in the using statement needed:using System.Data.Entity.ModelConfiguration;Now create an
IdentityUserRoleConfigurationclass underneath the class you just created
public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
}
}//<----Closing curly brace of fileIn this class, add the following method
public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
public IdentityUserRoleConfiguration()
{
HasKey(iur => iur.UserId);
}
}
}//<----Closing curly brace of fileGo back up under the
Create()method that is within theApplicationDbContextclassUnderneath the
Create()method, but still within theApplicationDbContextclass, add aNotespropertypublic static ApplicationDbContext Create() { return new ApplicationDbContext(); } public DbSet<Note> Notes { get; set; } //<--- Add this }You will have an error on
<Note>until the beginning of part 3, don't worry about it for now.
Directly underneath the
Notesproperty, but still within theApplicationDbContextclass, type override and then a space.Auto-complete should provide you with choices, choose
OnModelCreatingand hit enter.Delete the
base.OnModelCreating(modelBuilder);and replace it with the following:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Conventions
.Remove<PluralizingTableNameConvention>();
modelBuilder
.Configurations
.Add(new IdentityUserLoginConfiguration())
.Add(new IdentityUserRoleConfiguration());
}
}CTRL .to bring in the using statement needed
Notes:
Here is all of the code we have added for context:
Next, we'll create the Note entity for our database.
Last updated