Tuesday, March 6, 2012

Moving ASP.Net Membership tables to production

Recently we were migrating our development database from local to Azure production db. The database has both application specific tables and aspnet membership tables.As usual the person who moved ignored all the development data by generating the table creation script alone with application specific seed data.

The system uses Azure ACS to have federated authentication from 3 identity providers. Google,Yahoo and our own custom identity provider using claims. The standard providers such as google and yahoo worked perfectly but the custom identity provider didn't. The error message shown was

The 'System.Web.Security.SqlMembershipProvider' requires a database schema compatible with schema version '1'. However, the current database schema is not compatible with this version. You may need to either install a compatible schema with aspnet_regsql.exe (available in the framework installation directory), or upgrade the provider to a newer version.

This clearly says we are missing something in the AspNet membership schema level. Not at all any seed data. But actually the issue is with seed data.ie we need to enable the features in the asp.net membership database. When we enable the features aspnet membership uses a table called aspnet_SchemaVersions to store the settings. This schema is confused with table schema in the error message. If you want to have more details you can try running the below query on a working aspnetdb.mdf.

SELECT feature,
       compatibleschemaversion,
       iscurrentversion
FROM   aspnet_schemaversions 


This is not our focus. Our focus is to get the issue resolved. Its simple we need to register the features into the asp net membership database which is ported to Azure. Open the SSMS and connect to Azure database and run the below queries.

EXEC [dbo].Aspnet_registerschemaversion
  N'Common',
  N'1',
  1,
  1

EXEC [dbo].Aspnet_registerschemaversion
  N'Role Manager',
  N'1',
  1,
  1

EXEC [dbo].Aspnet_registerschemaversion
  N'Membership',
  N'1',
  1,
  1 


If you are getting any error executing this sps make sure you have all the aspnet membership related stored procedures in the database. If not available run the membership related scripts which are available in your machine. You can find the scripts here in the location.

[InstallDrive]:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallCommon.sql

If you don't want to take chance of running the scripts one by one use the tool aspnet_regsql

No comments: