所以我试图使用以下初始化程序
为我的数据库设定种子public class DbInitializer : DropCreateDatabaseAlways<IAMContext>
{
protected override void Seed(IAMContext context)
{
WebSecurity.InitializeDatabaseConnection("IAMContext",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = Roles.Provider;
var membership = Membership.Provider;
if (!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
if (!WebSecurity.UserExists("test"))
{
WebSecurity.CreateUserAndAccount("test", "password");
}
if (!roles.GetRolesForUser("test").Contains("Admin"))
{
roles.AddUsersToRoles(new[] { "test" }, new[] { "admin" });
}
context.Products.Add(new Product
{
Id = 1,
Name = "Homunculi",
Price = 85,
LongDescription = "Happens when failed human transmutations occur",
ShortDescription = "Failed Xmute",
media = "CD"
});
base.Seed(context);
}
}
我总是得到一个例外,我必须首先调用WebSecurity.InitializeDatabaseConnection。看起来我已经有了,所以我有点困惑。任何帮助都会很棒。我应该提一下我是webforms身份验证的新手。感谢您的理解和答案!
答案 0 :(得分:0)
查看此article on seeding and customizing SimpleMembership。它可能会为您提供有关SimpleMembership数据库正确初始化的一些提示。本文讨论了修改SimpleMembership的初始化方式比使用 InitializeSimpleMembershipAttribute 更直接。您需要调用几个地方初始化数据库,因为播种并不总是发生,并且它需要在您的种子方法中才能工作。因此,安全方法是在调用之前首先检查它是否已经初始化,如下面的代码片段所示。
if (!WebMatrix.WebData.WebSecurity.Initialized)
WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("SimpleSecurityConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);