2016-07-26 2 views
0

저는 ASP.NET MVC를 처음 접했고 특히 현재 등록 프로세스를 수정해야합니다. 성공적으로 등록되면 사용자 이메일과 사용자 이름을 사용하여 다른 데이터베이스의 다른 테이블에 INSERT를 수행합니다. 지금까지 내가 찾은 다른 것들은 내가 필요로하지 않는 데이터 검증을 등록하거나 변경하기 위해 필드를 추가하는 것이기 때문에 이것을 구체적으로 달성 할 수있는 방법을 찾지 못했습니다.ASP.NET MVC 5 사용자 등록을 사용자 정의하여 다른 데이터베이스에 연결

또한이 작업을 수행하는 저장 프로 시저를 만들었지 만 어떤 방법이 더 좋은지 잘 모릅니다.

IdentityModel.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ApplicationDbContext() 
      : base("UsersConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
    } 

    public class ZivoyDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ZivoyDbContext() 
      : base("DefaultConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ZivoyDbContext Create() 
     { 
      return new ZivoyDbContext(); 
     } 
    } 

그리고 심지어이 작업을 수행하는 방법을 모르는 계정 컨트롤러

// POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

        //Insert into another table 
        ZivoyDbContext.Users.Add(); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 
        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

        return RedirectToAction("Index", "Home"); 
       } 
       AddErrors(result); 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

, 사람이 어떻게 작동하는지 설명해 주 시겠어요? SQL Server에 연결하는 MVC 5를 검색했지만 거의 모든 대답은 단일 연결을 사용하는 단일 컨트롤러 또는 ADO.NET을 사용하는 단일 컨트롤러에 해당합니다.

+0

실제 질문은 무엇입니까? 작동하지 않는 것은 무엇입니까? – Rik

+0

@Rik 다른 데이터베이스의 다른 테이블에 사용자 데이터를 삽입하는 방법을 모르겠다. 시도한 코드는 아무 것도하지 않지만 다른 예제에서 본 것을 시도했다. 어디서 INSERT INTO ... 부분을 만들어야합니까? –

+0

이 튜토리얼을 확인하십시오 - EF6이라고 생각합니다 - 데이터를 추가하는 데 도움이되는지 확인하려면 다음을 수행하십시오. http://www.entityframeworktutorial.net/EntityFramework4.3/add-entity-using-dbcontext.aspx – EtherDragon

답변

0

나는이 문제가 사용자가 ZivoyDbContext를 통해 사용자를 추가하려하지만 사용자 데이터를 메서드로 전달하지 않는다고 생각합니다.

은 오랫동안 ZivoyDbContext 데이터베이스 및 테이블이 작성되고,이 유사한 전화를 사용

// Create a new User object using (all of the required) properties of the 
// new User you have just created via the Register method 
ZivoyDbContext.Users.Add(new ApplicationUser 
       { 
        UserName = user.UserName, 
        PasswordHash = user.Password, 
        Email = user.Email 
       } 
); 

자동으로 컨텍스트에서 수행되지 않은 경우 SaveChanges를()에 잊고 ...

IdentityDbContext는 평범한 EF 컨텍스트에서는 그렇지 못하다는 것을 기억하십시오. 상황에 따라 추가, 업데이트, 삭제, 찾기 등의 방법으로 데이터베이스를 최신 상태로 유지할 수 있습니다. IdentityDbContext에는 사용자와 로그인을 관리하기위한 많은 요소가 있습니다. 다른 데이터베이스는 사용자의 복사본을 저장하려는 경우에는 필요하지 않습니다. IdentityDbContext에서 컨텍스트를 상속 받으면 해당 기능을 모두 사용하지 않는 것이 좋습니다.

+0

이것은 마침내 그것을했다. 고맙다. –

관련 문제