1

I created a website and published it to Azure. Now I want to protect some pages with authentication so I added the following code in the method Seed of Migrations.Configuration and published it to Azure.

However, the code is run even on local. The table AspNetRoles is still empty on both local and Azure SQL server. I tried to use Update-Database -Script -SourceMigration:0 to generate all the SQL statements but there is no SQL inserting the initial data to these tables of Asp.Net identity.

// Create role
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
const string roleName = "CanEdit";

var role = roleManager.FindByName(roleName);
if (role == null)
{
    role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(roleName);
    var roleresult = roleManager.Create(role);
}

var memberEmails = Properties.Settings.Default.CanEditMembers.Split(';');
foreach (var email in memberEmails)
{
    var user = userManager.FindByName(email.Trim());
    if (user != null)
    {
        var rolesForUser = userManager.GetRoles(user.Id);
        if (!rolesForUser.Contains(role.Name))
        {
            var result = userManager.AddToRole(user.Id, role.Name);
        }
    }
}

Basically I want to insert 'CanEdit' to the AspNetRoles (I guess) and insert some other values (Users and their associations with the role) to these AspNetUser.... tables. Some of my Controls are already decorated with attribute [Authorize(Roles = "CanEdit")].

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • What type of migration are you running? Does it automatically run at application startup? Note that no SQL would be output - once the migration has run, the `seed` is executed (note - it is run **every time** you run the migration, even if there is no migration pending). – Brendan Green Aug 07 '15 at 04:54
  • So you don't see the seed being called via the powershell console output when you run `Update-Database`. What do you see? – Brendan Green Aug 07 '15 at 05:06
  • Ah - now I get it. It's not possible for EF to convert the logic that you have in your Seed method into an SQL Script. You'd have to run the migration proper. – Brendan Green Aug 07 '15 at 05:07
  • How to do it? Basically I want to insert 'CanEdit' to the AspNetRoles (I guess) and insert some other values to these AspNetUser.... tables. Some of my Controls are already decorated with attribute `[Authorize(Roles = "CanEdit")]`. – ca9163d9 Aug 07 '15 at 05:10
  • There's nothing stopping you from writing a manual SQL Script that performs the given inserts into the database. – Brendan Green Aug 07 '15 at 05:37
  • Some fields have column Id with type of varchar(128). Not sure what to fill in. And which tables need to be inserted. – ca9163d9 Aug 07 '15 at 05:41
  • Take a look at you existing data - I'd guess that they are `GUID`s, which you can create in TSQL via `NEWID()` – Brendan Green Aug 07 '15 at 06:14
  • The existing aspnetroles table is empty. – ca9163d9 Aug 07 '15 at 06:16
  • Unless you have customised the `Id`s, they will be `Guid`s. – Brendan Green Aug 07 '15 at 06:29
  • You may be running into this issue: http://stackoverflow.com/questions/23574591/seed-database-for-identity-2/23861288#23861288 – Steve Greene Aug 07 '15 at 18:33

0 Answers0