0

웹 응용 프로그램에서 클래스 라이브러리 프로젝트로 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"); 

Rolesstatic class에서 오는

?

답변

1

는 직접 역할에 액세스 할 수 있어야하지만, SimpleMembership 공급자를 사용할 때 그것을 권하고 싶지 않다. 즉, 귀하의 프로젝트에서 참조 된 System.Web 어셈블리가 있습니까? 당신은 당신이 차이가 꽤가 볼 수 SimpleRoleProvider역할 의 정의를 비교하면

var roles = (WebMatrix.WebData.SimpleRoleProvider)Roles.Provider; 

    if (!roles.RoleExists("Admin")) 
    { 
     roles.CreateRole("Admin"); 
    } 

:

역할 공급자를 얻기위한 바람직한 방법은 같은 것을 할 것입니다. SimpleRoleProvider은 사용자 지정 공급자를 구현할 때 역할에 대한 전체 인터페이스를 구현하지 않아도됩니다. 역할에서 직접 호출하는 경우 일부 메소드에서 "구현되지 않음"예외가 발생할 수 있습니다. SimpleRoleProvider 은 SimpleMembership을 사용할 때 유용 할 수있는 추가 메서드/속성도 제공합니다.

+0

안녕하세요. 나는 System.Web을 참조하지 않았다! 신속하게 SimpleRoleProvider를 Roles와 비교했습니다. 역할은 SimpleRoleProvider보다 많은 메소드/속성을 제공합니다. 또한, 왜 그것이 선호되는 방식입니까? 왜 역할이 존재하는지 if 문에 직접 체크하지 않는 이유는 무엇입니까? – Yustme

+0

내가 대답 한대로 SimpleRoleProvider는 필요하지 않기 때문에 구현 된 모든 메서드가없는 "사용자 지정"역할 공급자입니다. 따라서 Role 대신 SimpleRoleProvider를 사용하면 실제로 구현 된 메소드 만 볼 수 있습니다. Role에서 구현되지 않은 메소드를 호출하면 예외가 발생합니다. 또한 SimpleRoleProvider는 SimpleMembership 사용과 관련된 Role에없는 추가 메서드를 제공합니다. –

+0

그래서 어느 것을 사용해야합니까? 간단한 하나 또는 역할? – Yustme

2

Web.config 파일의 system.web 섹션에 roleManager 요소를 추가 했습니까? the MSDN page on Roles에서 :

, 당신의 ASP.NET 응용 프로그램에 대한 역할 관리를 활성화하려면 다음 예제와 같이 응용 프로그램의 Web.config 파일의 system.web 섹션의으로 roleManager 요소를 사용합니다.

섹션은 다음과 같습니다

<roleManager defaultProvider="SqlProvider" 
    enabled="true" 
    cacheRolesInCookie="true" 
    cookieName=".ASPROLES" 
    cookieTimeout="30" 
    cookiePath="/" 
    cookieRequireSSL="false" 
    cookieSlidingExpiration="true" 
    cookieProtection="All" > 
    <providers> 
     <add 
     name="SqlProvider" 
     type="System.Web.Security.SqlRoleProvider" 
     connectionStringName="SqlServices" 
     applicationName="SampleApplication" /> 
     </providers> 
    </roleManager> 
+0

안녕하세요, 저는 rolemanager 및 멤버 자격 profider를 app.config에 포함 시켰습니다. 하지만 고마워! – Yustme

0

클래스 라이브러리 프로젝트에서 시드 방법을 사용하고 있습니다.

당신은 이러한 참조의 역할, 회원을 해결하는 두 개의 참조

1. System.Web 
2. System.Web.ApplicationServices 

을 추가해야합니다.