我Migrations
已从 Web 应用程序移植到类库项目。一切正常,除了我不能调用static class Roles
.
我已经包含了using System.Web.Security;
它们所在的名称空间Roles
。
这是 Configuration.cs 文件的内容:
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");
}
}
}
错误信息是:
The name 'Roles' does not exist in the current context
我在这里想念什么?
[编辑]
我一直在做更多的研究,看来我必须创建一个对象SimpleRoleProvider
才能访问该RoleExists
方法。
但是为什么我必须这样做呢?为什么我不能只使用:
if (Roles.RoleExists("Administrator"))
Roles.CreateRole("Administrator");
Roles
来自一个static class
?