0

I'm trying to do this using Entity Framework 5 in C# in an MVC3 web application:

// Local DB connection
var dbId = String.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0}.mdf;Database=Tests;Integrated Security=True;Pooling=False;Connect Timeout=30;User Instance=True", _dbPath);
_connection = new SqlConnection(dbId);

Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());

// Reset database if the file exists
var ctx = new MyContext(_connection);
ctx.Database.CreateIfNotExists();
ctx.Database.Initialize(true);
ctx.SaveChanges();

return _connection;

This works for my tests, but not when I run the web application. When I run the web application, I get:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I see in a few other questions people have suggested removing the 'User Instance' from the connection string, but that results in:

CREATE DATABASE permission denied in database 'master'.

Let me be entirely clear on my goal here: I'm trying to have the web application create database instances (sqlexpress) on demand.

Creating the database instances in advance is not a solution.

I'm quite puzzled about why some kind of special permission is needed when the app worker is the one accessing and creating the database.

How can you do this?

Using sqlite this is a trivial operation, and I'm starting to feel like using Entity Framework (and thus SQL Server Express) was mistake and I should have gone with fluent nhibernate and sqlite instead.

2
  • Are you using IIS to host your MVC app? If so, what account is the app pool using. It is possibly a permissions issue on this account. Commented Oct 3, 2012 at 2:13
  • Yes. On my local copy of IIS I'm using the "ASP.NET v4.0" worker; this hasn't gone anywhere near production, obviously, because its broken, but we have a separate worker for the application on prod. Commented Oct 3, 2012 at 2:18

1 Answer 1

1

This article http://support.microsoft.com/kb/2002980 suggests that it is a file permission issue for the default account of the "ASP.Net V4.0" App pool, "ApplicationPoolIdentity" and suggests changing it to use "Network Service" instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately that didn't seem to help. Changed to use the "NetworkService" identity, ran iisreset, and still I see: "CREATE DATABASE permission denied in database 'master'."
I take that previous comment back; after restarting the machine, this did actually fix it! iisreset isn't sufficient on its own; perhaps something about restarting sql server was also involved, but a full restart fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.