3

I just want to quickly spin up the default database in my development environment.

How is the easiest way to get around this problem?

user880954
  • 7,317
  • 6
  • 22
  • 20
  • Does the login you are using to connect to the db has dbcreator role? –  Oct 06 '11 at 09:57

4 Answers4

3

I once faced this problem and resolved it. The key is using SQL authentication instead of Windows'. This is the clearest way to specify the default db.

Try connection string in this way:

<add name="MFCConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MFC.mdf;Initial Catalog=MFC;Integrated Security=false;User ID=sa;Password=123"
  providerName="System.Data.SqlClient" />

remember to set default db of sa from master to MFC, and Integrated security = false. BTW, sa is normally disabled, so enable and test it in sql server management studio first.

cheny
  • 2,545
  • 1
  • 24
  • 30
  • I know it's an old post, but blew me away that changing the connection string property to `Integrated Security=false` was the answer for me. Setup was a local server running IIS with SQLEXPRESS on that server. Could not get the SQL server to allow my account to create a database even though everything was set. Great post for me. Thanks.! – Alan Mar 07 '19 at 23:02
2

This may be of use to anybody stumbling across this question, as I did, when looking for an answer to the error. These steps should be all you need and I've copied code in you can paste to get it running quickly.

I'm using Code First, tried using 'create-database' but got the error in the title. Closed and re-opened (as Admin this time) - command not recognised but 'update-database' was so used that. Same error.

Here are the steps I took to resolve it:

1) Opened SQL Server Management Studio and created a database "Videos"

2) Opened Server Explorer in VS2013 (under 'View') and connected to the database.

3) Right clicked on the connection -> properties, and grabbed the connection string.

4) In the web.config I added the connection string

   <connectionStrings>
<add name="DefaultConnection"
  connectionString="Data Source=MyMachine;Initial Catalog=Videos;Integrated Security=True" providerName="System.Data.SqlClient"
  />
  </connectionStrings>

5) Where I set up the context, I need to reference DefaultConnection:

using System.Data.Entity;

namespace Videos.Models
{
public class VideoDb : DbContext
{
    public VideoDb()
        : base("name=DefaultConnection")
    {

    }

    public DbSet<Video> Videos { get; set; }
}
}

6) In Package Manager console run 'update-database' to create the table(s).

Remember you can use Seed() to insert values when creating, in Configuration.cs:

        protected override void Seed(Videos.Models.VideoDb context)
        {
        context.Videos.AddOrUpdate(v => v.Title,
            new Video() { Title = "MyTitle1", Length = 150 },
            new Video() { Title = "MyTitle2", Length = 270 }
            );

        context.SaveChanges();
        }
VictorySaber
  • 3,084
  • 1
  • 27
  • 45
2

Run your application under account which has permission to create database on your development SQL server. If you are using SQL authentication specify credentials for SQL login in your connection string which has this permission. By default admin account specified during SQL server installation has this permission but you can add it to other logins as well.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • +1 - Also try to run Visual Studio as Administrator. Even if your own account has administrator privilege I have found it necessary to do this on some machines when developing with a local database. – ChrisBD Oct 06 '11 at 10:02
0

I have the same problem with EF 6.0 and code first. If you have multiple projects with different connection strings and running update-database from the package manager console even if you select the right default project, Visual studio reads the connection string from the start up project and so if there is no connection on that start up project then the error is permission denied..

You can solve it by set the right project as start up project (just for updte database).

Paolo Vigori
  • 1,612
  • 1
  • 18
  • 32