我正在使用EF5 Code First。当我尝试使用DbContext
时,我收到以下错误:
底层提供程序在Open上失败。
InnerException
是:
无法打开登录请求的数据库“MyDb”。登录失败。 用户“ sa ”登录失败。
数据库不存在。在下面的代码中,我检查数据库是否存在,以便根据我的域更改创建它。
if (context.Database.Exists() ||
context.Database.CompatibleWithModel(false))
{
context.Database.Delete();
}
连接字符串:
connectionString="Data Source=.\sql2012; Initial Catalog=MyDb; Persist Security Info=True; User ID=sa; Password=*****"
更新: DbContext用法:
public class DomainDbContext : DbContext
{
public DomainDbContext()
: base("WebMarkerConnection")
{
}
public IDbSet<ServiceUser> ServiceUsers { get; set; }
public IDbSet<Favorite> Favorites { get; set; }
public IDbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ServiceUserConfig());
modelBuilder.Configurations.Add(new FavoriteConfig());
modelBuilder.Configurations.Add(new CategoryConfig());
base.OnModelCreating(modelBuilder);
}
}
任何建议都会有所帮助。