每次构建项目时,我都会遇到问题.初始值设定项似乎在运行,但当涉及到第一个查询时,它会随着下面的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,所以"支持的"可能不是这个词:)