日期:2014-05-20  浏览次数:20914 次

Code First连接远程数据库,在数据库 'master' 中拒绝了 CREATE DATABASE 权限。

        static void Main(string[] args)
        {
            var builder = new ModelBuilder();
            builder.Configurations.Add(new BookConfiguration());
            builder.Entity<Person>();
            builder.Entity<Publisher>().Property(p => p.Name).IsRequired().HasMaxLength(50);

            var model = builder.CreateModel();

            Console.Write("Enter an ISBN for the new book: ");
            var isbn = Console.ReadLine();

            Console.Write("Enter a title for the new book: ");
            var title = Console.ReadLine();

            // Data access with DbContext
            using (var context = new SimpleBookCatalog("CodeFirstWalkthrough", model))
            {
                var book = new Book
                {
                    ISBN = isbn,
                    Title = title,
                    FirstPublished = DateTime.Today,
                    IsFiction = false,
                    Author = new Author { FirstName = "Rowan", LastName = "Miller" },
                    Publisher = new Publisher { Name = "EF Books" }
                };

                context.Books.Add(book);   //报错:在数据库 'master' 中拒绝了 CREATE DATABASE 权限。
                context.SaveChanges();
            }
         }
数据库连接配置:
<add name="MyConnection"
       providerName="System.Data.SqlClient"
       connectionString="data Source=192.168.253.31\SQLEXPRESS; initial catalog=CodeFirstWalkthrough;User ID=sa;Password=uf_1234;Persist Security Info=True;User Instance=True" />

创建数据库不成功,在master中拒绝create,不知道怎么解决啊,唉!

------解决方案--------------------

<add name="MyConnection"
  providerName="System.Data.SqlClient"
  connectionString="data Source=192.168.253.31\SQLEXPRESS; initial catalog=CodeFirstWalkthrough;User ID=sa;Password=uf_1234;Persist Security Info=True;User Instance=True" />


你是不是用本机sql开发,然后发布到服务器,修改了一下服务器地址和加上User ID=sa;Password=uf_1234?不能这样的,默认本机是用windows用户登录SQLEXPRESS数据库的文件,而不是数据库服务器。