简体   繁体   中英

EF7 migration generator is ignoring context parameter

I have recently migrated my EF7 project from DotNet Core 1.0.0 to Version 1.1.0, now I have trouble with creating the database migrations.

In my project I have multiple contexts, the tool dotnet-ef is providing a command line parameter to distinct between these contexts in order to create a separate migration for each context.

When I try to create the migrations for the context "AuditLogContext" with the command

dotnet ef migrations add Initial --context 'Some.Namespace.AuditLogContext' --output-dir './Migrations/AuditLogMigrations'

then it fails, telling me

The name 'Initial' is used by an existing migration.

when I'm trying to create the migration with another name, say Initial2 , the migration get created but for the wrong context.

In the folder

./Migrations/AuditLogMigrations

are new files named

20170212122451_Initial2.cs

20170212122451_Initial2.Designer.cs

CoreContextModelSnapshot.cs

I expect the file AuditLogContextModelSnapshot.cs instead.

Does anybody have solved this issue before?

I found the reason for this problem.

I have multiple contexts in my project, every context expects parameters. So migrations builder expects a factory for every context.

In my case one class, implements all factories, now there is a bug that the concrete order of the interfaces is used to determine which migrations are created.

public class Program : IDbContextFactory<CoreContext>, IDbContextFactory<AuditLogContext>, IDbContextFactory<LockContext>

This snippet will create migrations for the CoreContext

public class Program :  IDbContextFactory<AuditLogContext>, IDbContextFactory<CoreContext>,IDbContextFactory<LockContext>

and the snippet will create migrations for the AuditLogContext.

It's a bit annoying but if you know how to create the migrations it's ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM