2016-08-22 1 views
0

다른 사용자 데이터를 다른 테이블에 추가 한 다음 AspNetUser (ID 프레임 워크)를 추가 할 수있는 솔루션을 얻으려고합니다. 사용자가 등록하고 로그인 할 때 한 번만이 새 테이블에서 데이터를 검색하려고합니다. 그래서 저는 ApplicationUser와 1 : 1 관계를 맺고 있습니다.사용자 정의 Db가있는 통합 ID 프레임

public class ApplicationUser : IdentityUser 
{ 
    //[ForeignKey("User")] 
    public Guid UserId { get; set; } 

    [Required] 
    public User User { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 


public class User 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid UserId { get; set; } 
    public DateTime BirthDate { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 

    public ApplicationUser AspNetUser { get; set; } 
} 

그리고 2 DB 컨텍스트가

public class MainDbContext: DbContext 
{ 
    public DbSet<Project> Projects { get; set; } 
    public DbSet<ProjectType> ProjectTypes { get; set; } 
    public DbSet<User> Users { get; set; } 

    public MainDbContext() 
     :base("MainDb") 
    { 

    } 
} 

그리고이 하나가 신원 프레임 워크의 내 상황입니다 있습니다.

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

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

왜 두 개의 데이터베이스가 필요합니까? 'MainDbContext'가'IdentityDbContext'를 상속받을 수 없습니까? 아는 한 DbContext의 객체에 같은 컨텍스트가 아닌 객체에 대한 탐색 속성을 만들 수 없습니다. – Alisson

+0

하나의 데이터베이스 만 있지만 두 개의 컨텍스트가 있습니다. 어떻게 작동하게 할 수 있습니까? ID 프레임 워크와 함께 작동하는 db에 사용자 정의 테이블을 추가해야합니다. 내가 상속받을 수있는 방법을 설명해 주시겠습니까? 그러면 db가 ID 프레임 워크 테이블을 만들 때 내 사용자 정의 테이블도 생성됩니다. – Letoir

+0

'ApplicationDbContext'와 마찬가지로, 제대로 대답 할 것입니다. – Alisson

답변

0

두 개의 DbContext을 사용할 필요가 없습니다.

public class MainDbContext: IdentityDbContext<User> 
{ 
    public DbSet<Project> Projects { get; set; } 
    public DbSet<ProjectType> ProjectTypes { get; set; } 

    public MainDbContext() 
     :base("MainDb") 
    { 

    } 
} 

또한 (당신이 정말 싶어하지 않는 한) 사용자 지정 속성에 대해 별도의 클래스를 만들 필요가 없습니다 : 당신은 당신의 주요 DbContext이 같은 IdentityDbContext을 상속 할 수 있습니다. 당신은 다음과 같이 할 수있는 :

public class User : IdentityUser 
{ 
    public DateTime BirthDate { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

두 가지 여기서주의 걸릴 :

  • 당신은 사용자의 ID 열을 필요로하지 않는, IdentityUser 부모 클래스가 이미 선언하기 때문이다.
  • IdentityDbContext이 이미 선언 했으므로 User에 대해 DbSet을 넣을 필요가 없습니다.

이것은 가장 간단한 시나리오입니다. 사용자 지정 속성에 대해 별도의 클래스를 만들 수도 있고 DbContexts으로 구분할 수도 있지만 이는 복잡한 시나리오를위한 것입니다. 가능한 한 가장 간단하게 시작/유지해야한다고 생각합니다.

관련 문제