所以我突然發現這個錯誤,我不知道如何解決它。 MyDomain\MyUser
列在服務器用戶下,但由於我使用CodeFIrst,數據庫尚未創建,因此我無法將MyDatabase
添加到用戶。CodeFirst:無法打開登錄請求的數據庫「MyDatabase」。登錄失敗。用戶'MyDomain MyUser'登錄失敗
這裏是我的代碼:
的ConnectionString:
<add name="MyDatabaseConnection" connectionString="Data Source=myServer; Database=MyDatabase.sdf;Trusted_Connection=True;Persist Security Info=True;Pooling=false" providerName="System.Data.SqlClient" />
代碼:
public class MyDbContext: DbContext
{
public MyDbContext()
: base("MyDatabaseConnection")
{
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new UserConfiguration());
}
}
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
ToTable("User");
Property(u => u.ID).HasColumnName("UserID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
protected void Application_Start()
{
Database.SetInitializer<MyDbContext>(new MyDbContextInitializer());
using (MyDbContext db = new MyDbContext())
{
db.Database.Initialize(false);
}
...
}
public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges<MyDbContext>
{
protected override void Seed(MyDbContextcontext)
{
base.Seed(context);
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection(ConfigurationManager.ConnectionStrings["MyDatabaseConnection"].ConnectionString, "System.Data.SqlClient", "User", "UserID", "UserName", true);
WebSecurity.CreateUserAndAccount("doe1", "password11", new { FirstName = "John", LastName = "Doe" });
}
}
我有MultipleActiveResultSets = True。刪除這個問題解決了類似的問題。 –