question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to database in migration

See original GitHub issue

In our project we used migrations in environment with data which we don’t want to lose. Actually we are forced to use some compression directly on model. I created migration where I connect to database and, using SqlClient etc., get, transform and save data.

Connection string is saved in config.json file. We also used another config file depend on environment. Is it any possibility to access to connection string from migration or just to environment specified by -e switch in dotnet ef cli?

EF Core version: 1.0.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yseriouscommented, Apr 2, 2019

Actually you can access your dbContext in a migration, see: https://stackoverflow.com/questions/42644253/can-i-inject-dependency-into-migration-using-ef-core-code-first-migrations Once you have your dbContext, you can probably access the connection-string through:

        var a = _dbContext.Database.GetDbConnection().ConnectionString;

(I actually use this to add new (non-seeding) data in a separate migration. However, be very careful when changing data this way. Your _dbContext.Save is executed before any (other) migrations are run.)

0reactions
bricelamcommented, Jan 28, 2017

One alternative would be to do this while the migrations are applying in your application:

using (var db = new MyDbContext())
{
    db.Database.OpenConnection();
    try
    {
        var migrator = db.GetService<IMigrator>();
        foreach (var migration in db.Database.GetPendingMigrations())
        {
            // TODO: Do things before each migration.
            // TODO: Access database using db.Database.GetDbConnection()
            migrator.Migrate(migration);
            // TODO: Do things after each migration.
        }
    }
    finally
    {
        db.Database.CloseConnection();
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate an Access database to SQL Server
Microsoft provides Microsoft SQL Server Migration Assistant (SSMA) to make migration easier. SSMA mainly migrates tables and select queries with no parameters.
Read more >
MS Access Database to SQL Server Migration in 6 Steps
To migrate some particular tables, Open the database, expand Tables, and then click on the check boxes next to the tables to transfer...
Read more >
Migrating from MS Access to SQL Server Made Easy 101
Right-click the database you wish to migrate in Access Metadata Explorer and choose Migrate Data. Note that if you want to migrate the...
Read more >
Migrate or Convert Access to SQL Server: Step-by-Step Process
How To Convert Access To SQL Server? · Step 1: Technical Discovery · Step 2: Microsoft SQL Server Migration Assistant (SSMA) Execution ·...
Read more >
Migrating a Microsoft Access Database to SQL Server
Microsoft provides a handy tool called the “Microsoft SQL Server Migration Assistant for MS Access” (SSMA) to assist in moving the database tables...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found