Cannot Use Identity Column Key Generation With Union-subclass Mapping For
Cannot Use Identity Column Key Generation With Union-subclass Mapping For 3,5/5 4601 reviews
-->
  • Feb 27, 2015 My Journey on Java - Ranga Reddy. Cannot use identity column key generation with mapping for. Cannot use identity column key generation with.
  • Jul 21, 2006 org.hibernate.MappingException: Cannot use identity column key generation with mapping for: concept.entity.Entity However: If I do annotate this @Id, I get this one: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: concept.entity.Entity.
  • Composite and Foreign Keys as Primary Key. This tutorial shows how the semantics of composite primary keys work and how they map to the database. General Considerations¶ Every entity with a composite key cannot use an id generator other than “ASSIGNED”.

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.

Identity and EF Core Migrations

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:

  1. Define or update a data model in code.
  2. Add a Migration to translate this model into changes that can be applied to the database.
  3. Check that the Migration correctly represents your intentions.
  4. Apply the Migration to update the database to be in sync with the model.
  5. Repeat steps 1 through 4 to further refine the model and keep the database in sync.

Use one of the following approaches to add and apply Migrations:

  • The Package Manager Console (PMC) window if using Visual Studio. For more information, see EF Core PMC tools.
  • The .NET Core CLI if using the command line. For more information, see EF Core .NET command line tools.
  • Clicking the Apply Migrations button on the error page when the app is run.

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:

  • Run Update-Database in PMC.
  • Run dotnet ef database update in a command shell.
  • Click the Apply Migrations button on the error page when the app is run.

Repeat the preceding steps as changes are made to the model.

The Identity model

Entity types

The Identity model consists of the following entity types.

Entity typeDescription
UserRepresents the user.
RoleRepresents a role.
UserClaimRepresents a claim that a user possesses.
UserTokenRepresents an authentication token for a user.
UserLoginAssociates a user with a login.
RoleClaimRepresents a claim that's granted to all users within a role.
UserRoleA join entity that associates users and roles.

Entity type relationships

The entity types are related to each other in the following ways:

  • Each User can have many UserClaims.
  • Each User can have many UserLogins.
  • Each User can have many UserTokens.
  • Each Role can have many associated RoleClaims.
  • Each 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.

Default model configuration

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:

Model generic types

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:

Customize the model

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:

  • Supplying entity and key types for the generic type parameters.
  • Overriding 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

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. Windows 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:

For

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:

Change the primary key type

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:

  1. If the database was created before the PK change, run Drop-Database (PMC) or dotnet ef database drop (.NET Core CLI) to delete it.

  2. After confirming deletion of the database, remove the initial migration with Remove-Migration (PMC) or dotnet ef migrations remove (.NET Core CLI).

  3. 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:

  4. 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.

  5. 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.

Add navigation properties

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.

Nhibernate Cannot Use Identity Column Key Generation With Union-subclass Mapping For

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.

Add all User navigation properties

Using the section above as guidance, the following example configures unidirectional navigation properties for all relationships on User:

Add User and Role navigation properties

Using the section above as guidance, the following example configures navigation properties for all relationships on User and Role:

Notes:

  • This example also includes the UserRole join entity, which is needed to navigate the many-to-many relationship from Users to Roles.
  • Remember to change the types of the navigation properties to reflect that ApplicationXxx types are now being used instead of IdentityXxx types.
  • Remember to use the ApplicationXxx in the generic ApplicationContext definition.

Add all navigation properties

Using the section above as guidance, the following example configures navigation properties for all relationships on all entity types:

Use composite keys

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.

Change table/column names and facets

Column

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:

Map to a different schema

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:

Lazy loading

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:

  • Installation of the Microsoft.EntityFrameworkCore.Proxies package.
  • A call to UseLazyLoadingProxies inside AddDbContext<TContext>.
  • Public entity types with 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.

Additional resources