Ama*_*ere 28 c# database entity-framework code-first ef-code-first
我每次构建时都试图运行我的项目时遇到问题.似乎初始化程序运行,但是当涉及第一个查询时 - 它会死于以下内容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.
Run Code Online (Sandbox Code Playgroud)
作为参考,我使用的是直接导入NuGet的EF Code First CTP4.连接到SQL Server 2008 R2
我想要发生的是,如果有任何模型修改,则重新创建数据库,并为查找表提供一些值.这些东西似乎都支持*开箱即用.
我的设置是这样的:
Global.asax中
protected void Application_Start()
{
Database.SetInitializer<CoreDB>(new CoreDBInitialiser());
// et al...
}
Run Code Online (Sandbox Code Playgroud)
CoreDB.cs
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));
}
}
Run Code Online (Sandbox Code Playgroud)
当它运行时,它会死在与此类似的行上,这基本上是创建数据库后遇到的第一个查询.
User data = (from u in _data.Users where u.Username == userName select u).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
我不认为的事情是:
providerName属性.<add name="CoreDB" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUsername;Password=ThePassword;" providerName="System.Data.SqlClient" />
该怎么办?
理想情况下,我想"不要考虑数据库模式".我希望它能像Scott Gu的优秀博客文章一样(并跟进使用现有数据库),事情刚刚解决并消失了.在大多数情况下,这是事实.这似乎是连接在某些时候没有关闭的问题,但我无法找到如何纠正这个问题.
一个小论坛/SO帖子做意味着我遇到的问题基本上是因为按计划,连接可能会保持开放的初始化不完全工作.其他地方的解决方案似乎只是"不创建自己的初始化程序",这不是最好的解决方案 - 但除非有人有任何想法,否则我可能不得不这样做直到CTP5.
*是的,我知道这是一个CTP,所以"支持"可能不是这个词:)
biz*_*zon 49
我知道答案是迟到的,但这个帖子在Goolge上很高,答案可能对某些人有用.问题是缺少凭据.要防止这种情况,您需要更改连接字符串以使其具有:
Trusted_Connection=False;Persist Security Info=True
Run Code Online (Sandbox Code Playgroud)
基于这篇文章
根据要求,我发表评论作为答案.
我的解决方案非常类似于bizon,其中Trusted_Connection和Persist Security Info需要更正,但我通过visual studio属性完成了它:
服务器资源管理器 - > 修改连接 - > 高级 - >然后将Persist Security Info和TrustServerCertificate都检查为True,并正确格式化连接字符串
