Saturday, August 30, 2014

Using integer PK instead of GUID, in ASP.NET Identity 2.1

For a new project, I wanted to use auto-incrementing Int keys for the userid (instead of the default GUID). Identity 2.x supports this change, and there are lots of guidelines online for the steps. The steps went mostly smoothly all the way, as far as the program modifications were concerned.

From the database aspect, though, some points to note are:

1. A "database migration" has to be done, to enable the system to build the tables correctly, with INT columns instead of nvarchar(128) for the Id column in various tables.

2. The Id columns in the tables DO NOT automatically get created as IDENTITY types - they get created as simple Int columns (even though the "Up" migration states "identity: true" for them). So it is essential to do these steps:

a. Run the program and try to create a new user - this will create the tables in the database, but will come up with a big error page, stating: Cannot insert the row because the Id column does not allow nulls.

b. Go to the database, and modify the Id column to set it to an Identity type, for the following tables: AspNetRoles and AspNetUsers.

Then run the program again, create a new user - everything should work as expected now.

No comments:

Post a Comment