ASP.NET MVC 发布到虚拟空间碰到了问题!在数据库 'master' 中拒绝了 CREATE DATABASE 权限。

「已注销」 2015-07-13 12:15:10
在数据库 'master' 中拒绝了 CREATE DATABASE 权限。
怎么破?
求教!
我知道虚拟空间肯定不能创建 数据库的,肯定是用已有的,我想问 下怎么才能用EF连接已有的数据库而不让其自行创建数据库谢谢!


...全文
181 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2015-07-13
  • 打赏
  • 举报
回复
引用 1 楼 findcaiyzh 的回复:
你在web.config中指定连接字符串(指向已经存在的数据库),code first就不会新建数据库了。 参考 http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database Importantly, though, the auto-create database option is just an option – it is definitely not required. If you point your connection-string at an existing database then EF “code first” will not try and create one automatically. The auto-recreate option also won’t be enabled unless you explicitly want EF to do this – so you don’t need to worry about it dropping and recreating your database unless you’ve explicitly indicated you want it to do so. For this blog post we will not auto-create the database. Instead, we’ll point at the existing Northwind database we already have. To do this we’ll add a “Northwind” connection-string to our web.config file like so:

connectionStrings>
      
    <add name="Northwind" 
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\northwind.mdf;User Instance=true" 
         providerName="System.Data.SqlClient" />
 
  </connectionStrings>
注意 链接字符串的name要匹配 你初始化DBContext时的名字,例如下面代码中的name=那个部分。

public class MyApplicationContext: DbContext
{
    public MyApplicationContext() : base("name=ConnectionStringName") { }

    public DbSet<Customer> Customers { get; set; }
}
好的,谢谢 ,我回去试试
Justin-Liu 2015-07-13
  • 打赏
  • 举报
回复
不用code first 连接现有数据库
  • 打赏
  • 举报
回复
现在的数据库连接字符串都不指定数据库的?
宝_爸 2015-07-13
  • 打赏
  • 举报
回复
你在web.config中指定连接字符串(指向已经存在的数据库),code first就不会新建数据库了。 参考 http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database Importantly, though, the auto-create database option is just an option – it is definitely not required. If you point your connection-string at an existing database then EF “code first” will not try and create one automatically. The auto-recreate option also won’t be enabled unless you explicitly want EF to do this – so you don’t need to worry about it dropping and recreating your database unless you’ve explicitly indicated you want it to do so. For this blog post we will not auto-create the database. Instead, we’ll point at the existing Northwind database we already have. To do this we’ll add a “Northwind” connection-string to our web.config file like so:

connectionStrings>
      
    <add name="Northwind" 
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\northwind.mdf;User Instance=true" 
         providerName="System.Data.SqlClient" />
 
  </connectionStrings>
注意 链接字符串的name要匹配 你初始化DBContext时的名字,例如下面代码中的name=那个部分。

public class MyApplicationContext: DbContext
{
    public MyApplicationContext() : base("name=ConnectionStringName") { }

    public DbSet<Customer> Customers { get; set; }
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧