Search code examples
c#asp.net-mvc-4entity-framework-migrationssimplemembership

The name 'Roles' does not exist in the current context


I have ported Migrations from a web app to a class library project. Everything works fine, except that I can't call the static class Roles.

I have included the namespace using System.Web.Security; which is where the Roles are located.

Here is the Configuration.cs file contents:

namespace _DataContext.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebMatrix.WebData;
    using System.Web.Security;

internal sealed class Configuration : DbMigrationsConfiguration<_DataContext.DataContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(_DataContext.DataContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //

        SeedMembership();
    }

    private void SeedMembership()
    {
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

        // doesn't work either:
        //var roles = (SimpleRoleProvider)Roles.Provider;
        //var membership = (SimpleMembershipProvider)Membership.Provider;

        if (Roles.RoleExists("Administrator"))
          Roles.CreateRole("Administrator");
    }
  }
}

Error message is:

The name 'Roles' does not exist in the current context

What am I missing here?

[Edit]

I have been doing some more research and it appears that i have to create an object from SimpleRoleProvider in order to access the RoleExists method.

But why do i have to do it this way? Why can't i just use:

if (Roles.RoleExists("Administrator"))              
   Roles.CreateRole("Administrator");

Roles comes from a static class?


Solution

  • You should be able to access Roles directly, but I would not recommend it when using the SimpleMembership providers. That being said, do you have the assembly System.Web referenced in your project?

    The preferred method for getting the role provider is to do something like this:

      var roles = (WebMatrix.WebData.SimpleRoleProvider)Roles.Provider;
    
      if (!roles.RoleExists("Admin"))
      {
          roles.CreateRole("Admin");
      }
    

    If you compare the definition of Roles versus SimpleRoleProvider you will see there is quite a bit of difference. It looks like the SimpleRoleProvider does not implement the complete interface for Roles, which is not required when implementing a custom provider. You may get a "not implemented" exception on some the methods if you call them directly from Roles. SimpleRoleProvider also provides additional methods/properties that can be useful when using SimpleMembership.