Efcore: Migration name cannot be the same as the DbContext class name

Created on 14 Nov 2018  Â·  5Comments  Â·  Source: dotnet/efcore

I have an ASP.NET Core Razor app that I was working on when I downloaded and installed the latest SDK in order to use updated NuGet packages. Since then I get the same error when attempting to perform migrations. My project already had one migration added, and the database successfully updated, for the Identity classes. When I then tried to add a new migration created by the scaffolder I got the following error:
System.ArgumentException: GenericArguments[0], 'Agency.Migrations.AgencyContext1', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory1[TContext]' violates the constraint of type 'TContext'. ---> System.TypeLoadException: GenericArguments[0], 'Agency.Migrations.AgencyContext1', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory1[TContext]' violates the constraint of type parameter 'TContext'.
at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.Instantiate(Type[] inst)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
--- End of inner exception stack trace ---
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextFactory(Type contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String name)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.&lt;Execute&gt;b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) GenericArguments[0], 'Agency.Migrations.AgencyContext1', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory1[TContext]' violates the constraint of type 'TContext'.

I then attempted to start over and deleted the database, removed the migration files for the Identity, and tried to add that migration. I received the same error. The command line I used for the initial (Identity) migration is:
Add-Migration Initial -Context:ApplicationContext1
I then attempted to add a NEW migration whose DbContext was generated as a result of me scaffolding new CRUD pages and I get the same error when I try to add that migration

I seriously need to resolve this soon! Help!!!


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

area-migrations closed-fixed punted-for-3.1 type-bug

Most helpful comment

I discovered what the problem is (was). It turns out that you cannot name the migration with the same name as the DbContext class. In other words, if my DbContext is ClientContext, I cannot use the command "Add-Migration ClientContext -Context:ClientContext". However, if I use "Add-Migration MyClientContext -Context:ClientContext" then it works fine. There was no need to use a IDesignTimeDbContextFactory class.

Perhaps some mention should be made of this little quirk in the documentation, or, better yet, modify the code to allow for it.

Consider this closed.

All 5 comments

I should also mention that after I started getting the errors when trying to Add-Migration, I updated VS 2017 to 15.9.0. That didn't help.

UPDATE: I made sure that all migration files were deleted and the connection strings in the appsettings file. Then, when I tried to rerun the initial migration for the Identity context, named AgencyContext1, I received the following error:
PM> Add-Migration Initial -Context:AgencyContext1
Unable to create an object of type 'AgencyContext1'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
I did not have to do this the first time I successfully ran the migration. What could possibly be wrong here?

I discovered what the problem is (was). It turns out that you cannot name the migration with the same name as the DbContext class. In other words, if my DbContext is ClientContext, I cannot use the command "Add-Migration ClientContext -Context:ClientContext". However, if I use "Add-Migration MyClientContext -Context:ClientContext" then it works fine. There was no need to use a IDesignTimeDbContextFactory class.

Perhaps some mention should be made of this little quirk in the documentation, or, better yet, modify the code to allow for it.

Consider this closed.

Let me guess, we generate this:

[DbContext(typeof(ClientContext))]
class ClientContext : Migration
{
}

...and of course, the C# compiler resolves the type in the attribute to be the current class, not the DbContext.

With above fix we now throw an exception if the migration name is the same as the context and ask the user to use a different migration name.

Was this page helpful?
0 / 5 - 0 ratings