asp.net Auto-generated SQLEXPRESS database: Failed to generate a user instance of SQL Server ( troubleshooting 1 )
Server Error in '/SamplesApp' Application.
Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed.
To thoroughly I log some repro steps.
I create a new application pool "SamplesAppPool" to host "SamplesApp".
The "SamplesAppPool" 's identity is assigned using the win7/winserver 2008 r2-recommended feature: "ApplicationPoolIdentity" , leaving "Load User Profile" as false currenctly.
Such setting is meant to create a special account with the same name as target pool to run W3WP.exe. For better security issue we no longer resort to Network Service. This is an improved model induced into new generation of windows OS.
However if we use default asp.net connection string settings to initialize a build-in asp.net security database ( assuming SQLEXPRESS instance installed ) , which means the conn string is like :
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
'"User Instance=true" means that SQLEXPRESS service needs a User Profile envrionment to perform some initialization works. Since the aspnet worker process is run in "SamplesAppPool" account, there should be a special user profile folder caring this account. However , since we previously set "Not load user profile", system doesn't at all create such a profile , thus no corresponding user profile folder created:
From Control Panel\System and Security\System\Advanced system settings panel we can get user profiles information
As you can see , the required "SamplesAppPool" profile is not maintained here.
Accordingly , "SamplesAppPool" profile folder does not exist in Users folder:
To get around the exception , "Load User Profile" needs setting as "true"
Once above settings changed , such profile is created in system immediately:
So far , the SQLEXPRESS-related information has not been established in the profile folder yet. We need trigger something to make account-specified sqlexpress folder constructed automatically. That is : do what I mentioned at the top. If you simply click "create user" button in a CreateUserWizard control without any custom connection settings in web.config , you may find App_Data/aspnetdb.mdf is successfully created. In addition , exploring "SamplesAppPool" profile folder will supply such a view:
To change application pool identity to build-in account such as "Network Service" without taking care of "Local User Profile"settings can solve the issue as well , because these build-in account auto-position to current logon user profile folder as is ensured by default and erect SQLExpress folder ( eg. if I logon as "zc0000" the folder is located at : C:\Users\zc0000\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS ). Nevertheless, this is no longer suggestive in new generation of windows OS ( window 7 , windows server 2008 r2 or latter ) . I think since Network Service has undertaken more and more tasks in the whole system, Microsoft designs such a new mechanism to help the powerful giant out of heavy load thus to have asp.net applications be more concentrative and secure.
Official knowledge base link concerning this issue:
Problems with SQL Server Express user instancing and ASP.net Web Application Projects