2013-08-12 2 views
0

그래서 나는 다음과 같은 초기화당신은 이미

public class DbInitializer : DropCreateDatabaseAlways<IAMContext> 
{ 
    protected override void Seed(IAMContext context) 
    { 

     WebSecurity.InitializeDatabaseConnection("IAMContext", 
      "UserProfile", "UserId", "UserName", autoCreateTables: true); 
     var roles = Roles.Provider; 
     var membership = Membership.Provider; 

     if (!roles.RoleExists("Admin")) 
     { 
      roles.CreateRole("Admin"); 
     } 
     if (!WebSecurity.UserExists("test")) 
     { 
      WebSecurity.CreateUserAndAccount("test", "password"); 
     } 

     if (!roles.GetRolesForUser("test").Contains("Admin")) 
     { 
      roles.AddUsersToRoles(new[] { "test" }, new[] { "admin" }); 
     } 

     context.Products.Add(new Product 
     { 
      Id = 1, 
      Name = "Homunculi", 
      Price = 85, 
      LongDescription = "Happens when failed human transmutations occur", 
      ShortDescription = "Failed Xmute", 
      media = "CD" 
     }); 


     base.Seed(context); 
    } 
} 

난 항상 내가 먼저 WebSecurity.InitializeDatabaseConnection를 호출해야하는 예외가 내 데이터베이스를 시드 시도하고있을 때 "WebSecurity.InitializeDatabaseConnection"예외를 호출해야합니다 . 내가 이미 가지고있는 것 같아서 나는 조금 혼란 스럽다. 어떤 도움이라도 좋을 것입니다. webforms 인증을 처음 사용한다고 언급해야합니다. 귀하의 이해와 답변을 주셔서 감사합니다!

답변

0

체크 아웃이 article on seeding and customizing SimpleMembership. SimpleMembership 데이터베이스의 적절한 초기화에 대한 힌트를 줄 수 있습니다. 이 기사에서는 SimpleMembership을 초기화하는 방법을 수정하는 방법에 대해 설명합니다.이 방법은 InitializeSimpleMembershipAttribute을 사용하는 것보다 간단합니다. seeding이 항상 발생하는 것은 아니므로 seed 메소드에서 작동해야하기 때문에 데이터베이스를 초기화하는 호출이 필요합니다. 그래서 안전한 방법은 먼저 다음 코드 조각과 같이 호출하기 전에 이미 초기화되었는지 확인하는 것입니다.

if (!WebMatrix.WebData.WebSecurity.Initialized) 
     WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("SimpleSecurityConnection", 
      "UserProfile", "UserId", "UserName", autoCreateTables: true); 
+0

userProfile에 문자열 대신 복잡한 객체를 어떻게 배치해야하는지 알지 못합니까? 이상한 오류가 계속 발생합니다. – Fitzpleasure

+0

Fitzpleasure - 복잡한 객체로 userProfile을 시드 할 때 코드의 예와 오류 유형에 대한 자세한 정보를 제공하는 다른 질문을 열고 대답 해 보겠습니다. –