동일한 데이터베이스로 마이그레이션을 사용하도록 여러 dbContext를 가져 오는
-
22-12-2019 - |
문제
샘플 MVC5 프로젝트를 기반으로 나는 마이그레이션을 처리하는 적절한 방법을 배우려고합니다.
루트 디렉토리에서 두 가지 컨텍스트가있는 "dbContext"라는 폴더가 있습니다.
첫 번째 항목 : IdentityContext.cs
public class IdentityContext : IdentityDbContext<ApplicationUser>
{
public IdentityContext()
: base("DefaultConnection")
{ }
}
.
Configuration.cs
로 IdentityMigration이라는 폴더가 있습니다. 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" }
// );
//
}
}
.
다음은 유사한 속성을 가진 MyContext가 있습니다.
"update-database"명령을 실행하려고하면이 오류 메시지가 나타납니다. 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 작업을 수행하는이를 수정하는 방법에 대한 아이디어는 무엇입니까?
해결책
제안 : 샘플 프로젝트를 수행하는 경우 마이그레이션을 배우는 것만 큼 DBContext 하나를 막아냅니다.IdentityDbContext<ApplicationUser>
두 번째 DBContext를 삭제 한 후 생성 한 기존 마이그레이션 폴더를 삭제하고 "이주 활성화"를 삭제합니다.이렇게하면 한 프로젝트에서 두 개의 DBContext를 사용하는 방법을 배우는 대신 마이그레이션을 계속해서 계속 해결하는 데 도움이됩니다.
또한, @lajos, 당신이 어떤 MVC를 이야기하고 있는지 모르겠지만, dbContext는 dbcontext 또는 identitydbcontext에서 상속받지 못했지만, DBContext가 상속받지 못했습니다.원하는 것은 프로젝트에서 "Enable-Migrations"를 발행 할 때 생성되는 마이그레이션 구성 클래스입니다.마이그레이션 및 시딩 데이터를 생성하는 데 사용됩니다.