ializeDatabaseConnection不与代码优先迁移

ializeDatabaseConnection不与代码优先迁移

本文介绍了WebSecurity.InitializeDatabaseConnection不与代码优先迁移配合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我首先使用WebSecurity和EF代码迁移.

In my project I use WebSecurity and EF code first migrations.

我有要与WebSecurity结合使用的自定义UserProfile类.

I have my custom UserProfile class I want to combine with WebSecurity.

我想在种子方法的迁移的Configuration类中为用户设置种子.

I want to seed users in migrations' Configuration class in Seed method.

所以我试试这个:

#1)
if (!Roles.RoleExists("admin"))
            Roles.CreateRole("admin");

if (!WebSecurity.UserExists(AdminUserName))
    WebSecurity.CreateUserAndAccount(
        AdminUserName,
        "admin",
        new {Email = "[email protected]"});

但是它抱怨我应该先打电话给WebSecurity.InitializeDatabaseConnection.

But it complains that I should call WebSecurity.InitializeDatabaseConnection first.

好,那很有意义.因此,我在Global.asax中添加了以下几行:

Ok, that makes sense. So I added the following lines to my Global.asax:

#2)
WebSecurity.InitializeDatabaseConnection(
    _connectionStringName,
    "UserProfiles",
    "Id",
    "UserName",
    autoCreateTables: true);

但以下几行除外:

#3)
var dbMigrator = new DbMigrator(_configuration);
dbMigrator.Update();

抛出错误:

嗯,这在迁移尝试创建WebSecurity刚创建的表时再次有意义.

Well, this makes sense again as migrations try to create the table that's just been created by WebSecurity.

我找到了一种解决方法:将#2)放在#1的正上方.比它有用.

I found out a workaround: I placed #2) right on top of #1). Than it worked.

  1. 迁移创建了UserProfiles表
  2. 将WebSecurity附加到现有的UserProfiles表并创建所需的其他表
  3. 种子在找到所需表并初始化WebSecurity时起作用.

问题是我不得不在种子方法内部初始化WebSecurity,这有点臭.

The problem is that I had to initialize the WebSecurity inside the seed method, which is kind of smelly.

我的问题是如何将WebSecurity.InitializeDatabaseConnection移回Global.asax?

My question is how to move WebSecurity.InitializeDatabaseConnection back to Global.asax?

推荐答案

您可以在 Global.asax 中编写此代码:

you can write this code in Global.asax :

if (!WebMatrix.WebData.WebSecurity.Initialized)
                WebSecurity.InitializeDatabaseConnection(_connectionStringName, "UserProfile", "UserId", "UserName", autoCreateTables: true);

然后通过使用迁移来播种,您可以通过这种方式进行操作,在这里您可以放置​​自定义字段,例如电子邮件,...:

then for seeding that using migration you do that this way, here you can put your customized fields like email,... :

private void SeedMemberShip()
{
    if (!WebMatrix.WebData.WebSecurity.Initialized)
        WebSecurity.InitializeDatabaseConnection(_connectionStringName, "UserProfile", "UserId", "UserName", autoCreateTables: true);
    var roles = (SimpleRoleProvider)Roles.Provider;
    var membership = (SimpleMembershipProvider)Membership.Provider;
    if (!roles.RoleExists("Admin"))
    {
        roles.CreateRole("Admin");
    }

    if (membership.GetUser(username, false) == null)
    {
        membership.CreateUserAndAccount(username, password);
    }

    if (!roles.GetRolesForUser(username).Contains("Admin"))
    {
        roles.AddUsersToRoles(new[] { username }, new[] { "Admin" });
    }
}

然后调用上面的方法是种子方法:

then call above method is seed method :

protected override void Seed(YourContext context)
        {

            SeedMemberShip();
        }

这篇关于WebSecurity.InitializeDatabaseConnection不与代码优先迁移配合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:34