2014-01-23 2 views
1

이 두 컨텍스트를 병합하는 데 문제가 있습니다. ApplicationDbContext와 DataContext를 단일 컨텍스트로 병합

나는 IdentityModels.cs에서 코드 줄 삭제 :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 

    } 
} 

내 DataContext.cs 안에이 구현을

public class DataContext : DbContext 
{ 
    public DataContext() 
     : base("name=DefaultConnection") 
    { 
    } 

    // Account tables 
    public DbSet ApplicationUsers { get; set; } 
    public DbSet Roles { get; set; } 
    public DbSet Tokens { get; set; } 
    public DbSet UserClaims { get; set; } 
    public DbSet UserLogins { get; set; } 
    public DbSet UserManagements { get; set; } 
    public DbSet UserRoles { get; set; } 
    public DbSet UserSecrets { get; set; } 


    // App Models 

    public DbSet<Course> Courses { get; set; } 
    public DbSet<Student> Students { get; set; } 

} 

문제 : 때 I "갱신 데이터베이스"그것은 코스 및 학생 테이블 만 만들었습니다.

질문 나는 성공적으로이 방법을 구현하는 경우 나는 IdentityDbContext 인터페이스는, 예를 들면 제공하는 좋은 방법을 모두 잃게됩니다 :

DbSet<Model> Name {get;set;}

: 당신은 사용할 필요가

var rm = new RoleManager<IdentityRole>(
      new RoleStore<IdentityRole>(new ApplicationDbContext())); 
     return rm.RoleExists(name); 

답변

0

을 대신

DbSet Model {get;set;} 

귀하의 질문에 대한 답변입니다. RoleStore에는 DbContext가 필요합니다 (IdentityDbContext는 DbContext에서 상속받습니다). 그럼에도 불구하고 여전히 roleManager를 사용할 수 있어야합니다.

6

여기 OkLib Nybo가 게시 한 해결책은 How can one put application users in the same context as the rest of the objects?입니다. https://github.com/onybo/Asp.Net-Identity-sample-app/tree/master/CustomUser/CustomUser

이 모델 폴더에서 구성 폴더를 다운로드하여 모델 폴더 안에 폴더를 배치 :

는 GitHub의에서 자신의 샘플 프로젝트로 이동합니다.

DataContext 파일 안에 이러한 구성 파일을 호출하여 데이터베이스를 빌드하는 코드 스 니펫을 넣습니다.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     if (modelBuilder == null) 
      throw new ArgumentNullException("modelBuilder"); 
       modelBuilder.Configurations.AddFromAssembly(typeof(Generic_Repo_Template.Models.Configurations.ApplicationUserConfiguration).Assembly); 
    } 

이제 데이터베이스가 데이터 컨텍스트 개체를 통해 이러한 테이블에 액세스 할 수없는 여전히 당신을 만들어집니다.

public class DataContext : DbContext 
{ 
    public DataContext() 
     : base("name=DefaultConnection") 
    { 
    } 

    // Account tables 
    public virtual IDbSet<ApplicationUser> Users { get; set; } 
    public virtual IDbSet<IdentityRole> Roles { get; set; } 
    public virtual IDbSet<IdentityUserClaim> Claims { get; set; } 

    // App Models 
    public DbSet<Course> Courses { get; set; } 
    public DbSet<Student> Students { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     if (modelBuilder == null) 
      throw new ArgumentNullException("modelBuilder"); 
     modelBuilder.Configurations.AddFromAssembly(typeof(AspNetRoleBasedSecurity.Models.Configurations.ApplicationUserConfiguration).Assembly); 
    } 
} 
+0

이 ApplicationDbContext에서 데이터베이스를 업데이트하려면 : 그래서 전체의 DataContext 파일은 다음과 같이 보일 것이다

 public virtual IDbSet<ApplicationUser> Users { get; set; } public virtual IDbSet<IdentityRole> Roles { get; set; } public virtual IDbSet<IdentityUserClaim> Claims { get; set; } 

을 : 당신의 데이터 컨텍스트를 통해이 테이블에 액세스 할 수 있도록하기 위해 코드 줄을 포함하는 것입니다 DataContext에에 패키지 관리자 및 유형 열 : 당신은 마이그레이션이 추가 모든 파일을 내부에서 삭제하여 오류가 발생하는 경우 마이그레이션이 가능--force (입력) 추가 마이그레이션을 를 (입력) 갱신 데이터베이스 (입력)하여Migrations 폴더로 이동 한 후 다시 시도하십시오. – user3037717

관련 문제