Hi I am working on an aspnet core app, which using ef core version :
"Microsoft.EntityFrameworkCore.Design": "1.1.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0"
and dotnet standard 1.6.1.
here is my scenaro:
- 4 dbcontext with only a database
- DBSet or anyother may shows in two or three contexts, for example AContext contains tableA, BContext also contains tableA. but none of them will include all tables as i want to the specific context focus on its purpose.
- many foreigne keys between tables
this causes the dup table in database, but to resolve this, i have done below
- create a MigrationDBContext to include all DBSet
- add migration on this MigrationDBContext
here are what i get
- successfully create the database and tables with right schema and name
- error when call : serviceScope.ServiceProvider.GetService().Database.Migrate(); error message: There is already an object 'A' named in the database.
unfortunitly, the update-database command in ef core with -v will not show the sql script, Script-Migration shows only the simple create sql statement.
my questions are
- how to debug such of this situation?
- Checked the migration file, there do have a down and up method, but this error seems like the ef core only call the up method in migration file without calling the down first, and result some dup as the table is still there. so, is there any switch to control the migration behavior?
The simplest way is create the one DbContext that will include all sets of entities and relations between them. After that you can separate storage logic with repositories. The hard way is create context-specific migrations for each context. And if you need to add set of entities, that exists in another context - you will need to create an empty migration for that context. This is a little bit dirty way.