每次构建项目时,我都会遇到问题.初始值设定项似乎在运行,但当涉及到第一个查询时,它会随着下面的InvalidOperationException个查询而消失.

This operation requires a connection to the 'master' database. Unable to create a
connection to the 'master' database because the original database connection has
been opened and credentials have been removed from the connection string. Supply
an unopened connection.

作为参考,我使用的是EF Code First CTP4,它是用NuGet直接导入的.连接到SQL Server 2008 R2

我希望发生的是,如果有任何模型修改,则重新创建数据库,并使用查找表的一些值作为种子.这两件事似乎都是开箱即用的.

我的设置如下所示:

Global.asax

 protected void Application_Start()
 {
    Database.SetInitializer<CoreDB>(new CoreDBInitialiser());
    // et al...
 }

CoreDB.反恐精英

public class CoreDB : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Login> Logins { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<RolePermission> RolePermissions { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }
    public DbSet<Setting> Settings { get; set; }
}

public class CoreDBInitialiser : RecreateDatabaseIfModelChanges<CoreDB>
{
    protected override void Seed(CoreDB context)
    {
        var settings = new List<Setting>
        {
            new Setting
            {
                SettingName = "ExampleSetting",
                SettingValue = "This is a sample setting value",
            }
        };

        settings.ForEach(d => context.Settings.Add(d));
    }
}

当它运行时,它在类似于下面的行上终止,这基本上是它在创建数据库后遇到的第一个查询.

User data = (from u in _data.Users where u.Username == userName  select u).SingleOrDefault();

我不这么认为:

  • It's not permissions:我已经删除了SQL Server中的实际数据库本身.应用程序大约在try 运行该查询的同时重新创建它(设置了初始值设定项,然后显然它会推迟创建,直到需要它).我还以Web.config中指定的用户身份登录到SQL Server,他们对数据库具有完全读/写访问权限.事实上,他们可能应该这样做,因为该帐户也会创建数据库.
  • The database is being created:删除数据库,它会自动重新创建fine.
  • The connection string is correctly defined,包括providerName属性.

<add name="CoreDB" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUsername;Password=ThePassword;" providerName="System.Data.SqlClient" />

  • 它不会是我的代码/逻辑中的错误appear,因为一旦查询成功失败,就是the code will start properly until the next time the application is rebuilt.这显然是可能的,而且无论如何我都可能不得不在我的代码中应用一个变通方法.:)

What to do?

理想情况下,我希望"不太考虑数据库模式".我希望它看起来像是在Scott Gu's excellent blog post年(working with existing databases),事情就这样解决了,然后就消失了.这在很大程度上是正确的.这似乎是某个时候连接没有关闭的问题,但是我找不到如何纠正这个问题.

few forum个左右的帖子确实暗示了我遇到的问题,基本上是因为初始化者没有完全按照计划工作,连接可能会被打开.其他地方的解决方案似乎只是简单地"不要创建自己的初始值设定项",这不是最好的解决方案——但除非有人有任何 idea ,否则我可能不得不这样做,直到CTP5.

*是的,我知道它是CTP,所以"支持的"可能不是这个词:)

推荐答案

我知道现在回答已经很晚了,但这篇文章在Goolge上很受欢迎,答案可能对某些人有用.问题是缺少凭据.要防止出现这种情况,您需要将连接字符串更改为:

Trusted_Connection=False;Persist Security Info=True

this article为基础

Database相关问答推荐

DBMS_RANDOM 是否默认安装?

如何高效地存储棋局?

多列上的全文索引如何工作?

The method setListAdapter(ArrayAdapter) is undefined for the type create

从 XML 读取数据

触发器内的多个插入/更新语句?

PHP + SQL Server - 如何设置连接字符集?

在 SQL 中与 NULL 值连接

如何通过 PHP 和 Linux 使用 pdo 连接到 mssql?

在 Heroku 生产站点上清除 Rails 应用程序数据库

SQL Server 2005 是否具有与 MySql 的 ENUM 数据类型等效的数据类型?

PDO SQL 状态00000但仍然错误?

PHP 可以与 MS SQL 数据库一起使用吗

如何在android中使用合同类?

从原始物理文件中恢复 postgreSQL 数据库

用 SQL 进行条件插入?

在 MYSQL 的子查询中使用 LIMIT 关键字的替代方法

如何修复 Postgres 上过时的 postmaster.pid 文件?

从 SQLite 导出到 SQL Server

清空数据库是什么意思?