21

I've attempted to add a "service based data base" (.mdf) to a project in an asp .net application. From there I've proceeded attempted to create an entity framework model file (.edmx).

When doing so I get the error:

An error occurred while connecting to the database. The database might be unavailable. An exception of type 'System.Data.SqlClient.SqlException' occurred. The error message is: 'Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.'.

I've got SQL Server 2008 R2 Express edition installed on my machine.

Any ideas would be most appreciated.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
eran otzap
  • 12,293
  • 20
  • 84
  • 139

6 Answers6

43

I have got the same error, it's fixed after the following change.

Deleting the folder C:\Users\User\AppData\Local\Microsoft\Microsoft SQL Server Data worked for me in Windows 7. Have to restart VS after deleting the folder.

Rajagopal 웃
  • 8,061
  • 7
  • 31
  • 43
4

I had this exact same problem with a C# Console App that was utilizing an .mdf file.

The issue disappeared once I granted NETWORK SERVICE full access to my root-level Visual Studio project directory (and sub directories).

Solution Details: Problems with SQL Server Express user instancing and ASP.net Web Application Projects

Bosco
  • 935
  • 10
  • 18
4

See this answer, which might work for you.

Based on your connection string, you might need to add "User Instance" attribute to it.

Community
  • 1
  • 1
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
1

I was facing same issue in VS 2019. if anyone still facing same issue then please make sure you have/do following things: 1. you should have Sql server Express installed on your m/c 2. should have SSDT installed in VS (in VS 2019- name sure to check this component while installing) for previous version; i guess you have to add this externally 3. add 'User Instance = True' to your connection string and one more thing and I think its optional - open VS and SQL in administrative mode

Ashu_90
  • 904
  • 7
  • 8
0

If you build your own custom membership solution, do not forget to add this line to your Web.config's AppSettings section:

<add key="enableSimpleMembership" value="false" />

otherwise you'll get the same error message

Yaron
  • 1,540
  • 3
  • 19
  • 33
0

I have same issue, but it only happens when I reboot the PC and run the application from a fresh start. If I then attempt to re-start the application (wo rebooting), it does connect and I have full access to the database. I tried to change the connection string with a long timeout (600, 6000, 60000), but that made no difference. Here is the connection string: " "

  • What have you tried to let the first run succeed ? Have you tried the answers here ? Just stating you have the same issue is no answer.. rather a Comment.. – Goodies Aug 21 '21 at 00:16
  • I indicated that I tried changing the Connection Timeout values, but that did not help. – user3044638 Aug 21 '21 at 16:08
  • I also should add, that on fresh boot, sometimes it will create the user instance, and the application works. This happens about 25% of the time that it will work on fresh boot. But 100% of the time when I kill that application and restart again, it will work. – user3044638 Aug 21 '21 at 17:30