2.2: Identity Models Setup
Identity Model
In the ElevenNote.Data assembly, open the
IdentityModels.cs
file. 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
ApplicationDbContext
function.Underneath the function, but still inside the namespace, create the following
IdentityUserLoginConfiguration
class 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 file
CTRL .
to bring in the using statement needed:using System.Data.Entity.ModelConfiguration;
Now create an
IdentityUserRoleConfiguration
class underneath the class you just created
public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
}
}//<----Closing curly brace of file
In this class, add the following method
public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{
public IdentityUserRoleConfiguration()
{
HasKey(iur => iur.UserId);
}
}
}//<----Closing curly brace of file
Go back up under the
Create()
method that is within theApplicationDbContext
classUnderneath the
Create()
method, but still within theApplicationDbContext
class, add aNotes
propertypublic 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
Notes
property, but still within theApplicationDbContext
class, type override and then a space.Auto-complete should provide you with choices, choose
OnModelCreating
and 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