Entity Framework Core - 2 Contexts in the same database - Second context tables are not created

entity-framework entity-framework-core

Question

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?

1
2
8/21/2017 10:14:58 AM

Accepted Answer

Actually I found only this way.

RelationalDatabaseCreator databaseCreator = 
    (RelationalDatabaseCreator) context.Database.GetService<IDatabaseCreator>();
databaseCreator.CreateTables();
2
8/21/2017 11:26:48 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow