D
D
Denis2016-04-10 13:39:00
ASP.NET
Denis, 2016-04-10 13:39:00

How to move tables to another db using EF Code First?

There are two online store databases: One is the main database (the context is inherited from DbContext), the other contains users ( ApplicationDbContext, is inherited from IdentityDbContext<ApplicationUser>). On both, migrations are configured, which I perform separately (according to this guide).
Today I faced the need to combine them into one database (hosting limits the number of databases), and therefore the question arose: How to move everything, for example, to the main database correctly without violating the "homeostasis" of EF Code First Migrations and Asp.Net Identity? Is it possible to combine contexts, given that they are inherited from different classes? What to do next? Or is everything much simpler (for example, 2 contexts in one database)? I ask you to describe step by step, if not actions, then at least the logic of the process that I need to do to solve the problem.
PS: Of course, the “handles” option is also suitable, after which Code First will not complain.
PPS: The store is only being written, there is only 1 user, so re-creating a table with users is not scary. If only everything worked out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-04-11
@senal

Several contexts in one database coexist remarkably well, you only need to specify your own key for each

internal sealed class Configuration : DbMigrationsConfiguration<Models.ApplicationDbContext>
    {
        public Configuration()
        {
            ...
            ContextKey = "ApplicationDbContextKeyName";
        }

go to named migrations i.e. create them like this:
add-migration -name "MigrationName"
to see/correct the changes if you wish. In this case, it will be necessary each time to indicate on the command line with which context we are working. So it's probably better to merge contexts. You can, for example, transfer all entities to the ApplicationDbContext, generate a migration, remove the creation of tables from it (so as not to lose data) and continue working as usual.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question