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

-->

爱悠闲 hibernate中Cannot use identity column key generation with mapping for错误. Hibernate中Cannot use identity column key generation with mapping for错误. Cannot use identity column key generation with mapping for.

by Tom FitzMacken

In Visual Studio 2013, the default web application uses a string value for the key for user accounts. ASP.NET Identity enables you to change the type of the key to meet your data requirements. For example, you can change the type of the key from a string to an integer.

This topic shows how to start with the default web application and change the user account key to an integer. You can use the same modifications to implement any type of key in your project. It shows how to make these changes in the default web application, but you could apply similar modifications to a customized application. It shows the changes needed when working with MVC or Web Forms.

Software versions used in the tutorial

  • Visual Studio 2013 with Update 2 (or later)
  • ASP.NET Identity 2.1 or later

To perform the steps in this tutorial, you must have Visual Studio 2013 Update 2 (or later), and a web application created from the ASP.NET Web Application template. The template changed in Update 3. This topic shows how to change the template in Update 2 and Update 3.

This topic contains the following sections:

Change the type of the key in the Identity user class

In your project created from the ASP.NET Web Application template, specify that the ApplicationUser class uses an integer for the key for user accounts. In IdentityModels.cs, change the ApplicationUser class to inherit from IdentityUser that has a type of int for the TKey generic parameter. You also pass the names of three customized class which you have not implemented yet.

You have changed the type of the key, but, by default, the rest of the application still assumes the key is a string. You must explicitly indicate the type of the key in code that assumes a string.

In the ApplicationUser class, change the GenerateUserIdentityAsync method to include int, as shown in the highlighted code below. This change is not necessary for Web Forms projects with the Update 3 template.

Add customized Identity classes that use the key type

The other Identity classes, such as IdentityUserRole, IdentityUserClaim, IdentityUserLogin, IdentityRole, UserStore, RoleStore, are still set up to use a string key. Create new versions of these classes that specify an integer for the key. You do not need to provide much implementation code in these classes, you are primarily just setting int as the key.

Add the following classes to your IdentityModels.cs file.

Change the context class and user manager to use the key type

In IdentityModels.cs, change the definition of the ApplicationDbContext class to use your new customized classes and an int for the key, as shown in the highlighted code.

The ThrowIfV1Schema parameter is no longer valid in the constructor. Change the constructor so it does not pass a ThrowIfV1Schema value.

Open IdentityConfig.cs, and change the ApplicationUserManger class to use your new user store class for persisting data and an int for the key.

In the Update 3 template, you must change the ApplicationSignInManager class.

Change start-up configuration to use the key type

In Startup.Auth.cs, replace the OnValidateIdentity code, as highlighted below. Notice that the getUserIdCallback definition, parses the string value into an integer.

If your project does not recognize the generic implementation of the GetUserId method, you may need to update the ASP.NET Identity NuGet package to version 2.1

You have made a lot of changes to the infrastructure classes used by ASP.NET Identity. If you try compiling the project, you will notice a lot of errors. Fortunately, the remaining errors are all similar. The Identity class expects an integer for the key, but the controller (or Web Form) is passing a string value. In each case, you need to convert from a string to and integer by calling GetUserId<int>. You can either work through the error list from compilation or follow the changes below.

The remaining changes depend on the type of project you are creating and which update you have installed in Visual Studio. You can go directly to the relevant section through the following links

For MVC with Update 2, change the AccountController to pass the key type

Open the AccountController.cs file. You need to change the following methods.

ConfirmEmail method

Disassociate method

Manage(ManageUserViewModel) method

LinkLoginCallback method

RemoveAccountList method

HasPassword method

You can now run the application and register a new user.

For MVC with Update 3, change the AccountController and ManageController to pass the key type

Open the AccountController.cs file. You need to change the following method.

ConfirmEmail method

SendCode method

Open the ManageController.cs file. You need to change the following methods.

Index method

RemoveLogin methods

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

AddPhoneNumber method

EnableTwoFactorAuthentication method

DisableTwoFactorAuthentication method

VerifyPhoneNumber methods

RemovePhoneNumber method

ChangePassword method

SetPassword method

ManageLogins method

LinkLoginCallback method

HasPassword method

HasPhoneNumber method

You can now run the application and register a new user.

For Web Forms with Update 2, change Account pages to pass the key type

For Web Forms with Update 2, you need to change the following pages.

Confirm.aspx.cx

RegisterExternalLogin.aspx.cs

Manage.aspx.cs

You can now run the application and register a new user.

For Web Forms with Update 3, change Account pages to pass the key type

For Web Forms with Update 3, you need to change the following pages.

Confirm.aspx.cx

RegisterExternalLogin.aspx.cs

Manage.aspx.cs

VerifyPhoneNumber.aspx.cs

AddPhoneNumber.aspx.cs

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

ManagePassword.aspx.cs

ManageLogins.aspx.cs

TwoFactorAuthenticationSignIn.aspx.cs

Run application

You have finished all of the required changes to the default Web Application template. Run the application and register a new user. After registering the user, you will notice that the AspNetUsers table has an Id column that is an integer.

If you have previously created the ASP.NET Identity tables with a different primary key, you need to make some additional changes. If possible, just delete the existing database. The database will be re-created with the correct design when you run the web application and add a new user. If deletion is not possible, run code first migrations to change the tables. However, the new integer primary key will not be set up as a SQL IDENTITY property in the database. You must manually set the Id column as an IDENTITY.

Other resources

  • Sample application with changed primary key