开发者

EF Code First - {"CREATE DATABASE permission denied in database 'master'."}

开发者 https://www.devze.com 2023-04-11 07:48 出处:网络
I just want to quickly spin up the default database in my development environment. How开发者_如何学Go is the easiest way to get around this problem?I once faced this problem and resolved it. The key

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

How开发者_如何学Go is the easiest way to get around this problem?


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.


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.


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();
        }


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号