I'm trying to use 2 contexts on the same database.
To create the database I'm using DbContext.Database.EnsureCreated()
The first EnsureCreated
call works fine, the second EnsureCreated
call does not work.
Some considerations
Profiling SQL Server I can see that EF Core checks for the existence of a table
IF EXISTS (
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE')
SELECT 1
ELSE
SELECT 0
then it runs the create table statement.
If there is a user table on the DB nothing is done...
Is there a way to force table creations from entities scaffolding?
Actually I found only this way.
RelationalDatabaseCreator databaseCreator =
(RelationalDatabaseCreator) context.Database.GetService<IDatabaseCreator>();
databaseCreator.CreateTables();
In my case it was because I failed to run add-migration SecondContext_Initial -Context SecondContext
so there was no migration to run. Both EnsureCreated()
(returned false) and Migrate()
(did nothing) failed to yield any clues to my neglect.