웹 응용 프로그램에서 클래스 라이브러리 프로젝트로 Migrations
을 이식했습니다. static class Roles
으로 전화 할 수 없다는 점을 제외하면 모든 것이 잘 작동합니다.'Roles'이름이 현재 컨텍스트에 없습니다.
여기에는 Roles
이있는 이름 공간 using System.Web.Security;
이 포함되었습니다.
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");
}
}
}
오류 메시지는 다음과 같습니다 : 여기
가 Configuration.cs 파일 내용입니다
The name 'Roles' does not exist in the current context
내가 무슨 말이냐?
[편집]
좀 더 많은 연구를하고있다 그리고 내가 RoleExists
방법을 액세스하기 위해 SimpleRoleProvider
에서 개체를 만들 필요가 나타납니다.
하지만 왜 이런 식으로해야합니까? 왜 난 그냥 사용할 수 없습니다
if (Roles.RoleExists("Administrator"))
Roles.CreateRole("Administrator");
Roles
가 static class
에서 오는
안녕하세요. 나는 System.Web을 참조하지 않았다! 신속하게 SimpleRoleProvider를 Roles와 비교했습니다. 역할은 SimpleRoleProvider보다 많은 메소드/속성을 제공합니다. 또한, 왜 그것이 선호되는 방식입니까? 왜 역할이 존재하는지 if 문에 직접 체크하지 않는 이유는 무엇입니까? – Yustme
내가 대답 한대로 SimpleRoleProvider는 필요하지 않기 때문에 구현 된 모든 메서드가없는 "사용자 지정"역할 공급자입니다. 따라서 Role 대신 SimpleRoleProvider를 사용하면 실제로 구현 된 메소드 만 볼 수 있습니다. Role에서 구현되지 않은 메소드를 호출하면 예외가 발생합니다. 또한 SimpleRoleProvider는 SimpleMembership 사용과 관련된 Role에없는 추가 메서드를 제공합니다. –
그래서 어느 것을 사용해야합니까? 간단한 하나 또는 역할? – Yustme