November Happy Hour will be moved to Thursday December 5th.

Migrate CMS11 to CMS12 project with a custom ApplicationUser

Vote:
 

Hello,

We're in the process of migrating a CMS11 project to CMS12. The project is using a custom ApplicationUser, let's call it "MyApplicationUser", that extends ApplicationUser and adds a couple of new properties. The CMS11 project is using EF migrations for the MyApplicationUser.

In the startup.cs ConfigureServices method I've added 

services.AddCmsAspNetIdentity<MyApplicationUser>(configureSqlServerOptions: o => o.MigrationsAssembly("MyProject.Cms"));

When running the CMS12 updated source code using the old CMS11 database I get this error:

info: EPiServer.Framework.Initialization.InitializationEngine[0]
      Initialization started
fail: EPiServer.Framework.Initialization.InitializationEngine[0]
      Initialize action failed for 'Initialize on class EPiServer.Data.DataInitialization, EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7'
      System.Data.DataException: Failed to update database during execution of statement 'CREATE TABLE [dbo].[AspNetRoleClaims] (
          [Id]         INT            IDENTITY (1, 1) NOT NULL,
          [ClaimType]  NVARCHAR (MAX) NULL,
          [ClaimValue] NVARCHAR (MAX) NULL,
          [RoleId]     NVARCHAR (128) NOT NULL,
          CONSTRAINT [PK_AspNetRoleClaims]
       PRIMARY KEY CLUSTERED ([Id] ASC),
          CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId]
       FOREIGN KEY ([RoleId])
        REFERENCES [dbo].[AspNetRoles] ([Id]) ON DELETE CASCADE
      )
      '

Searching the web I found a couple of blogposts on migrating to aspnetidentity in CMS12, for example GetaDigital, The challenges I faced during the migration from cms version 11 to 12, aspnetidentity migration and How to customise identity user model in cms 12 but it's a bit unclear on what addresses shortcomings in previous versions of CMS12 and what is the best practice when migrating.

I haven't run the upgrade assistant as we created a new project from scratch. Neither have I applied any EF migrations (I tried the migration adjustements suggested in the GetaDigital blogpost, but that failed - it might be outdated, idk).

If possible, can anyone share what the best practice is when migrating aspnetidentity with a custom user to CMS12? Preferably I still want to use the users and roles existing in the CMS11 database.

#331110
Edited, Oct 08, 2024 15:29
Vote:
 

When migrating, your table might already exist in your database. We are also using custom user called "CommerceIdentityUser" and we have only added few columns to the "AspNetUsers" table and map them using the below line of code.

services.AddCmsAspNetIdentity<CommerceIdentityUser>()

#332240
Oct 31, 2024 20:54
Vote:
 

Solved.

Turns out that when creating the [dbo].[AspNetRoleClaims] table there is a constraint CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] that is looking for the primary key in the table [dbo].[AspNetRoles], but the Id column was not set as pk for some reason. When setting it to pk the necessary tables were created correctly.

#333259
Nov 21, 2024 21:44
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.