Reputation: 91
Based on a sample MVC5 project I'm trying to learn the proper way to handle migrations.
In the root directory I have a folder called "DbContexts" with two Contextes.
First one: IdentityContext.cs
public class IdentityContext : IdentityDbContext<ApplicationUser>
{
public IdentityContext()
: base("DefaultConnection")
{ }
}
then I have a folder called IdentityMigrations with the 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" }
// );
//
}
}
Then I have MyContexts with similar properties.
When I try to run the "Update-Database" command I get this error message: 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'.
Any idea on how to fix this or a better way to do this DbContext stuff?
Upvotes: 1
Views: 1700
Reputation: 9252
Suggestion: If you are just doing a sample project to learn migrations as you say, stick to one DbContext. Keep it simple - merge your entities into one DbContext that inherits from IdentityDbContext<ApplicationUser>
Delete your existing migrations folders you've created - and re "enable-migrations" after deleting your second DbContext. This will help you to continue learning migrations, instead of learning how to use two DbContext's in one project.
Also, @Lajos, I'm not sure which MVC you are talking about, but my DbContext's have never inherited from DbMigrationsConfiguration - they inherit from DbContext, or IdentityDbContext. What you are referring to is the MigrationsConfiguration class that gets generated when issuing "enable-migrations" on a project. It is used for generating migrations and seeding data.
Upvotes: 2