1

샘플 MVC5 프로젝트를 기반으로 마이그레이션을 처리하는 올바른 방법을 배우려고합니다.동일한 데이터베이스에서 마이그레이션을 사용하기 위해 여러 DbContext 가져 오기

루트 디렉터리에 두 개의 컨텍스트가있는 "DbContexts"라는 폴더가 있습니다.

먼저 하나 IdentityContext.cs

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

는 내가 그럼 난 유사한 특성을 가진 MyContexts을 가지고있는 Configuration.cs

internal sealed class Configuration : DbMigrationsConfiguration<TryFive.Web.DbContexts.IdentityContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
     MigrationsDirectory = @"DbContexts\IdentityMigrations"; 
    } 

    protected override void Seed(TryFive.Web.DbContexts.IdentityContext 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" } 
     // ); 
     // 
    } 
} 

와 IdentityMigrations라는 폴더가 있습니다. The type 'TryFive.Web.DbContexts.IdentityContext' does not inherit from 'System.Data.Entity.Migrations.DbMigrationsConfiguration'. Migrations configuration types must extend from 'System.Data.Entity.Migrations.DbMigrationsConfiguration'.

이 문제를 해결하는 방법에 대한 아이디어 나이 DbContext 물건을 할 수있는 더 좋은 방법 : 나는 "업데이트-데이터베이스"를 실행하려고하면

는 명령이 오류 메시지가?

답변

2

제안 : 말하는대로 마이그레이션을 배우기위한 샘플 프로젝트를 수행하는 중이라면 하나의 DbContext를 고수하십시오. 간단하게 유지 - 엔터티를 하나의 DbContext로 병합 IdentityDbContext<ApplicationUser>

생성 한 기존 마이그레이션 폴더를 삭제하고 두 번째 DbContext를 삭제 한 후에 "enable-migrations"를 다시 사용하십시오. 이렇게하면 하나의 프로젝트에서 두 개의 DbContext를 사용하는 방법을 배우는 대신 마이그레이션을 계속 학습하는 데 도움이됩니다.

또한 @Lajos, 내가 말하는 MVC에 대해 잘 모르겠지만 내 DbContext는 DbMigrationsConfiguration에서 상속 된 적이 없습니다. DbContext 또는 IdentityDbContext에서 상속됩니다. 당신이 말하는 것은 프로젝트에서 "enable-migrations"를 발행 할 때 생성되는 MigrationsConfiguration 클래스입니다. 마이그레이션 및 시드 데이터 생성에 사용됩니다.

+0

그래, 기본 DbContext에 내 모델을 추가해도 괜찮습니까? 내가 본 모든 샘플은 ASP.NET ID를 기본값으로 유지하고 동일한 데이터베이스를 호출하는 자체 DbContext를 만들고 DbContexts를 모두 받아 들일 수 있도록 마이그레이션을 지시하는 문제가있었습니다. – Kaleet

+0

네, 괜찮을 겁니다. 기본적으로 DbContext를 기본으로 제공하므로 필요에 따라 수정할 수 있지만 두 번째를 만들지 않아도됩니다. 나는 별개의 IdentityContext와 다른 컨텍스트를 사용하는 것에 대한 게시물을 보았지만 특정 시나리오를 다루지 않는 한 너무 복잡합니다. – Jack