Skip to content

DatabaseCreator - Check for tables from model rather than just any models. #2874

@hkoestin

Description

@hkoestin

When having two different DbContext instances, e.g. A and B, both having different tables, e.g. A has a1 and a2, B has b1 and b2, a call to context.Database.EnsureCreated() does not really work, when both of the context instances point to the same physical DB.

Only the tables of the first context will be created in the database. The tables from the second context will be ignored. Thus, the first action on the second context usually fails with an exception, that certain objects/tables do not exist.

So, if you call contextA.EnsureCreated() first, tables a1 and a2 will be created. That is fine. However, when you want to use the same DB for contextB as well, contextB.EnsureCreated() will not work anymore, because the "HasTables" method will simply say "true", because there are OTHER tables (namely a1 and a2 from context A) existing in the DB, but not b1 and b2.

The creator should also work in this case in my opinion.

HasTables should check, if the tables for the CURRENT context-model are given or not.
CreateTables should then create the tables for the CURRENT context, if needed.

Would this be possible?
Is this actually possible without any big side-effects?

See also similar issue for SQL CE:
ErikEJ/EntityFramework.SqlServerCompact#205

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions