gpt4 book ai didi

sql-server - CodeFirst : Cannot open database "MyDatabase " requested by the login. 登录失败。用户 'MyDomain\MyUser' 登录失败

转载 作者:行者123 更新时间:2023-12-02 05:23:07 26 4
gpt4 key购买 nike

所以我突然收到这个错误,我不知道如何解决它。 MyDomain\MyUser 列在服务器用户下,但由于我使用的是 CodeFIrst,数据库尚未创建,所以我无法将 MyDatabase 添加到用户。

这是我的代码:

连接字符串:

<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" });
}
}

最佳答案

  • 打开 IIS 管理控制台
  • 打开应用程序池节点
  • 选择与您的网站对应的应用程序池
  • 右键单击“应用程序池”并选择“高级设置”
  • 在流程模型部分选择身份
  • 选择本地系统

enter image description here

关于sql-server - CodeFirst : Cannot open database "MyDatabase " requested by the login. 登录失败。用户 'MyDomain\MyUser' 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com