我正在尝试迁移仅在运行时才知道的数据库,这意味着我无法使用包管理器控制台来更新数据库。此外,它不仅仅是一个,而是许多数据库,但它们都是相同的模式:)
我正在使用 Ninject 在DbContext
对象上生成和注入连接字符串。上下文构造函数如下所示:
public class MyEntities : DbContext
{
public MyEntities(string database) : base(database) { }
/* Properties code omitted. */
}
实例化上下文的方法如下:
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;
}
当检索到上下文时,该方法会创建一个连接字符串并将其作为参数传递给MyEntities
. 我还在方法中指定了我想要的迁移类型(在这种情况下MigrateDatabaseToLatestVersion
)。
迁移代码如下:
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 */ }
}
当我运行应用程序时,出现以下错误:
Cannot find the object "AccountsInMonths" because it does not exist or you do not have permissions.
迁移所做的是将列的类型AccountsInMonths
从更改int
为long
。
这是一个迁移错误,因为堆栈跟踪有对其的调用。此时我只能认为问题可能是权限,因为表存在。其他可能性是连接字符串上的某种问题。请问,有人有这方面的线索吗?提前致谢!
PS:如果不清楚,我可以在问题中添加更多信息。