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

问题描述

限时删除!!

在我的项目中,我使用WebSecurity和EF code首先迁移。

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

我有我的自定义用户配置类我想与WebSecurity结合起来。

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

我要种子用户迁移配置类种子的方法。

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

但是,除了下面几行:

But than the following lines:

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

抛出错误:

已存在名为'的UserProfiles'在数据库中的对象。

好了,这又是有道理的迁移尝试创建一个刚刚创建的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?

推荐答案

你可以这样写code。在的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不与code首先迁移合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 18:51