Roles and Membership installed to local SQL Express 2008 MDF file

Just had an hour or so’s worth of ball-ache from this issue!  Working on an MVC2 site, i wanted to move the default membership out of the aspnet mdf file and into the sites main MDF file.  This should be a pretty simple web.config change, and a matter of running aspnet_regsql against the database.  I read an article mentioning that you can do this straight from the command line to your MDF file, but I had no success with this method- it simply created another database for me with a name equal to the full path of the database i had requested.  So I found I had to attach my database to my local sqlexpress instance, then run it against that;  so in sql;

EXEC sp_attach_db 'mydb', N'E:\_Dev\5imply\5imply-MVC2\BackOffice\App_Data\5imply.mdf'

Then you can just run aspnet_regsql in the same way you would for any other database; In my example I just wanted Membership, roles and profiles

C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regsql.exe -S .\SQLEXPRESS -d mydb -A mrp -E

Start adding the following features:




Then once you’re done, you can detach again;

exec sp_detach_db 'mydb'

Also- one other gotcha I found was that I had to do all this using SQL Management studio, having started it using “Run as Administrator”- I’m not sure if this is something specific to Windows 7, as  I would have expected to already be an administrator?  But I spent ages wondering why SQL refused to attach my database- Just kept getting an error “create file encountered operating system error 5″ I eventually ran procmon and found it just didn’t have permisisons to the file under the account it was running under.

  1. No comments yet.
(will not be published)