Reputation: 1852
I'm trying to migrate a database that is only known at runtime, which means that I cannot use the Package Manager Console to update the database. Also it's not just one, but many databases, but it is the same schema for all of them :)
I'm using Ninject to generate and inject the connection string on the DbContext
object. The context constructor looks just like this:
public class MyEntities : DbContext
{
public MyEntities(string database) : base(database) { }
/* Properties code omitted. */
}
The method to instantiate a context follows:
public MyEntities GetDatabase(string databaseName, string connectionString)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
builder.InitialCatalog = databaseName;
MyEntities context = this._kernel.Get<MyEntities>(new ConstructorArgument(
"database", builder.ConnectionString));
Database.SetInitializer<MyEntities>(
new MigrateDatabaseToLatestVersion<MyEntities, MyEntitiesMigrationConfiguration>("MyEntities"));
return context;
}
When a context is retrieved the method creates a connection string and passes it as parameter to the constructor of MyEntities
. Also I specify in the method the type of migration I want (in this case MigrateDatabaseToLatestVersion
).
The migration code follows:
public partial class MyAccountInMonth : DbMigration
{
public override void Up()
{
AlterColumn("AccountsInMonths", "MovementDebtMonth", c => c.Long(nullable: false));
AlterColumn("AccountsInMonths", "MovementCreditMonth", c => c.Long(nullable: false));
AlterColumn("AccountsInMonths", "BalanceMonth", c => c.Long(nullable: false));
AlterColumn("AccountsInMonths", "MovementDebtAccumulated", c => c.Long(nullable: false));
AlterColumn("AccountsInMonths", "MovementCreditAccumulated", c => c.Long(nullable: false));
AlterColumn("AccountsInMonths", "BalanceAccumulated", c => c.Long(nullable: false));
}
public override void Down() { /* Code omitted */ }
}
When I run the application, the following error occurs:
Cannot find the object "AccountsInMonths" because it does not exist or you do not have permissions.
What the migration does is change the type of the column AccountsInMonths
from int
to long
.
It is a migration error, because the stack trace has calls to it. At this moment I can only think that the problem can be permissions because the table exists. Other possibility is some kind of problem on the connection string. Please, anybody has a clue for this? Thanks in advance!
PS: if it is not clear I can add more information to the question.
Upvotes: 5
Views: 4404
Reputation: 4776
I Had the several issue. Here is the solution:
1) Make your Configuration
as a public class:
public sealed class Configuration : DbMigrationsConfiguration<YourContextClassHere>
2) Make your Seed() method as a public
2) Add anywhere the code below, this will apply the latest migration and updates your db:
Configuration configuration = new Configuration();
configuration.ContextType = typeof(YourContextClassHere);
var migrator = new DbMigrator(configuration);
// This will update the schema of the DB
migrator.Update();
// This will run Seed() method
configuration.Seed(new YourContextClassHere());
Upvotes: 4
Reputation: 4150
You can use the Package Manager Console with Update-Database
. It has a switch to specify connection string - either connection string itself ($ConnectionString
and $ConnectionProviderName
) or named one from config ($ConnectionStringName
).
Or you can use DbMigrator
class to handle it yourself in code (similar to what's actually migrate.exe
doing).
Upvotes: 1
Reputation: 33910
To migrate a database outside of Visual Studio and the Package Manager Console, use the accompanying migrate.exe tool that is located in the EntityFramework nuget package. It basically let you do the same migration from a command line.
Upvotes: 2