WebSecurity.InitializeDatabaseConnection doesn't cooperate with code first migrations

家住魔仙堡 提交于 2019-12-24 14:27:07

问题


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

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

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

So I try this:

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

if (!WebSecurity.UserExists(AdminUserName))
    WebSecurity.CreateUserAndAccount(
        AdminUserName,
        "admin",
        new {Email = "admin@mindmap.pl"});

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

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

throw the error:

There is already an object named 'UserProfiles' in the database.

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

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

  1. Migrations created the UserProfiles table
  2. WebSecurity attached to the existing UserProfiles table and created other tables it needed
  3. Seeds works as they found the needed tables and WebSecurity was initialized.

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

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


回答1:


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


来源:https://stackoverflow.com/questions/19214205/websecurity-initializedatabaseconnection-doesnt-cooperate-with-code-first-migra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!