By Arthur Vickers
The accepted answer (although old) is not quite correct. Using GenerationType.IDENTITY does not imply that table generation is used by the DB. The javadoc for GenerationType.IDENTITY says: 'Indicates that the persistence provider must assign primary keys for the entity using a database identity column.'
ASP.NET Core Identity provides a framework for managing and storing user accounts in ASP.NET Core apps. Identity is added to your project when Individual User Accounts is selected as the authentication mechanism. By default, Identity makes use of an Entity Framework (EF) Core data model. This article describes how to customize the Identity model.
Before examining the model, it's useful to understand how Identity works with EF Core Migrations to create and update a database. At the top level, the process is:
Use one of the following approaches to add and apply Migrations:
ASP.NET Core has a development-time error page handler. The handler can apply migrations when the app is run. Production apps typically generate SQL scripts from the migrations and deploy database changes as part of a controlled app and database deployment.
When a new app using Identity is created, steps 1 and 2 above have already been completed. That is, the initial data model already exists, and the initial migration has been added to the project. The initial migration still needs to be applied to the database. The initial migration can be applied via one of the following approaches:
Update-Database
in PMC.dotnet ef database update
in a command shell.Repeat the preceding steps as changes are made to the model.
The Identity model consists of the following entity types.
Entity type | Description |
---|---|
User | Represents the user. |
Role | Represents a role. |
UserClaim | Represents a claim that a user possesses. |
UserToken | Represents an authentication token for a user. |
UserLogin | Associates a user with a login. |
RoleClaim | Represents a claim that's granted to all users within a role. |
UserRole | A join entity that associates users and roles. |
The entity types are related to each other in the following ways:
User
can have many UserClaims
.User
can have many UserLogins
.User
can have many UserTokens
.Role
can have many associated RoleClaims
.User
can have many associated Roles
, and each Role
can be associated with many Users
. This is a many-to-many relationship that requires a join table in the database. The join table is represented by the UserRole
entity.Identity defines many context classes that inherit from DbContext to configure and use the model. This configuration is done using the EF Core Code First Fluent API in the OnModelCreating method of the context class. The default configuration is:
Identity defines default Common Language Runtime (CLR) types for each of the entity types listed above. These types are all prefixed with Identity:
IdentityUser
IdentityRole
IdentityUserClaim
IdentityUserToken
IdentityUserLogin
IdentityRoleClaim
IdentityUserRole
Rather than using these types directly, the types can be used as base classes for the app's own types. The DbContext
classes defined by Identity are generic, such that different CLR types can be used for one or more of the entity types in the model. These generic types also allow the User
primary key (PK) data type to be changed.
When using Identity with support for roles, an IdentityDbContext class should be used. For example:
It's also possible to use Identity without roles (only claims), in which case an IdentityUserContext<TUser> class should be used:
The starting point for model customization is to derive from the appropriate context type. See the Model generic types section. This context type is customarily called ApplicationDbContext
and is created by the ASP.NET Core templates.
The context is used to configure the model in two ways:
OnModelCreating
to modify the mapping of these types.When overriding OnModelCreating
, base.OnModelCreating
should be called first; the overriding configuration should be called next. EF Core generally has a last-one-wins policy for configuration. For example, if the ToTable
method for an entity type is called first with one table name and then again later with a different table name, the table name in the second call is used.
Custom user data is supported by inheriting from IdentityUser
. It's customary to name this type ApplicationUser
:
Use the ApplicationUser
type as a generic argument for the context:
There's no need to override OnModelCreating
in the ApplicationDbContext
class. EF Core maps the CustomTag
property by convention. However, the database needs to be updated to create a new CustomTag
column. To create the column, add a migration, and then update the database as described in Identity and EF Core Migrations.
Microsoft website delivers the trial version of this particular window to you. After download and install it to the particular system, and you will feel that it is inactivated version or a trial version window. One can download the Microsoft Windows 7 from its official website and install it. It is to notify you that many websites offer you to provide you the activated version. And it does not require any hard system requirements.Windows 7 keygen is an optimal tool to activate windows 7 professional edition.
Update Pages/Shared/_LoginPartial.cshtml and replace IdentityUser
with ApplicationUser
:
Update Areas/Identity/IdentityHostingStartup.cs or Startup.ConfigureServices
and replace IdentityUser
with ApplicationUser
.
In ASP.NET Core 2.1 or later, Identity is provided as a Razor Class Library. For more information, see Scaffold Identity in ASP.NET Core projects. Consequently, the preceding code requires a call to AddDefaultUI. If the Identity scaffolder was used to add Identity files to the project, remove the call to AddDefaultUI
. For more information, see:
A change to the PK column's data type after the database has been created is problematic on many database systems. Changing the PK typically involves dropping and re-creating the table. Therefore, key types should be specified in the initial migration when the database is created.
Follow these steps to change the PK type:
If the database was created before the PK change, run Drop-Database
(PMC) or dotnet ef database drop
(.NET Core CLI) to delete it.
After confirming deletion of the database, remove the initial migration with Remove-Migration
(PMC) or dotnet ef migrations remove
(.NET Core CLI).
Update the ApplicationDbContext
class to derive from IdentityDbContext<TUser,TRole,TKey>. Specify the new key type for TKey
. For example, to use a Guid
key type:
In the preceding code, the generic classes IdentityUser<TKey> and IdentityRole<TKey> must be specified to use the new key type.
In the preceding code, the generic classes IdentityUser<TKey> and IdentityRole<TKey> must be specified to use the new key type.
Startup.ConfigureServices
must be updated to use the generic user:
If a custom ApplicationUser
class is being used, update the class to inherit from IdentityUser
. For example:
Update ApplicationDbContext
to reference the custom ApplicationUser
class:
Register the custom database context class when adding the Identity service in Startup.ConfigureServices
:
The primary key's data type is inferred by analyzing the DbContext object.
In ASP.NET Core 2.1 or later, Identity is provided as a Razor Class Library. For more information, see Scaffold Identity in ASP.NET Core projects. Consequently, the preceding code requires a call to AddDefaultUI. If the Identity scaffolder was used to add Identity files to the project, remove the call to AddDefaultUI
.
The primary key's data type is inferred by analyzing the DbContext object.
The AddEntityFrameworkStores method accepts a TKey
type indicating the primary key's data type.
If a custom ApplicationRole
class is being used, update the class to inherit from IdentityRole<TKey>
. For example:
Update ApplicationDbContext
to reference the custom ApplicationRole
class. For example, the following class references a custom ApplicationUser
and a custom ApplicationRole
:
Register the custom database context class when adding the Identity service in Startup.ConfigureServices
:
The primary key's data type is inferred by analyzing the DbContext object.
In ASP.NET Core 2.1 or later, Identity is provided as a Razor Class Library. For more information, see Scaffold Identity in ASP.NET Core projects. Consequently, the preceding code requires a call to AddDefaultUI. If the Identity scaffolder was used to add Identity files to the project, remove the call to AddDefaultUI
.
Register the custom database context class when adding the Identity service in Startup.ConfigureServices
:
The primary key's data type is inferred by analyzing the DbContext object.
Register the custom database context class when adding the Identity service in Startup.ConfigureServices
:
The AddEntityFrameworkStores method accepts a TKey
type indicating the primary key's data type.
Changing the model configuration for relationships can be more difficult than making other changes. Care must be taken to replace the existing relationships rather than create new, additional relationships. Quickbooks 2015 key generator torrent. In particular, the changed relationship must specify the same foreign key (FK) property as the existing relationship. For example, the relationship between Users
and UserClaims
is, by default, specified as follows:
The FK for this relationship is specified as the UserClaim.UserId
property. HasMany
and WithOne
are called without arguments to create the relationship without navigation properties.
Add a navigation property to ApplicationUser
that allows associated UserClaims
to be referenced from the user:
The TKey
for IdentityUserClaim<TKey>
is the type specified for the PK of users. In this case, TKey
is string
because the defaults are being used. It's not the PK type for the UserClaim
entity type.
Now that the navigation property exists, it must be configured in OnModelCreating
:
Notice that relationship is configured exactly as it was before, only with a navigation property specified in the call to HasMany
.
The navigation properties only exist in the EF model, not the database. Because the FK for the relationship hasn't changed, this kind of model change doesn't require the database to be updated. This can be checked by adding a migration after making the change. The Up
and Down
methods are empty.
Using the section above as guidance, the following example configures unidirectional navigation properties for all relationships on User:
Using the section above as guidance, the following example configures navigation properties for all relationships on User and Role:
Notes:
UserRole
join entity, which is needed to navigate the many-to-many relationship from Users to Roles.ApplicationXxx
types are now being used instead of IdentityXxx
types.ApplicationXxx
in the generic ApplicationContext
definition.Using the section above as guidance, the following example configures navigation properties for all relationships on all entity types:
The preceding sections demonstrated changing the type of key used in the Identity model. Changing the Identity key model to use composite keys isn't supported or recommended. Using a composite key with Identity involves changing how the Identity manager code interacts with the model. This customization is beyond the scope of this document.
To change the names of tables and columns, call base.OnModelCreating
. Then, add configuration to override any of the defaults. For example, to change the name of all the Identity tables:
These examples use the default Identity types. If using an app type such as ApplicationUser
, configure that type instead of the default type.
The following example changes some column names:
Some types of database columns can be configured with certain facets (for example, the maximum string
length allowed). The following example sets column maximum lengths for several string
properties in the model:
Schemas can behave differently across database providers. For SQL Server, the default is to create all tables in the dbo schema. The tables can be created in a different schema. For example:
In this section, support for lazy-loading proxies in the Identity model is added. Lazy-loading is useful since it allows navigation properties to be used without first ensuring they're loaded.
Entity types can be made suitable for lazy-loading in several ways, as described in the EF Core documentation. For simplicity, use lazy-loading proxies, which requires:
public virtual
navigation properties.The following example demonstrates calling UseLazyLoadingProxies
in Startup.ConfigureServices
:
Refer to the preceding examples for guidance on adding navigation properties to the entity types.