English 中文(简体)
《实体框架法》第一版:
原标题:Entity Framework Code-First: Migrate a database known only at runtime

I m试图迁移一个数据库,该数据库只是在运行时间才知道的,这意味着我无法利用包裹管理人Console更新数据库。 此外,它不仅是一个数据库,而且有许多数据库,而且对所有这些数据库来说都是相同的图象:

I m 采用Ninject 生成和注入<代码>DbContext的链接。 背景构造者认为:

public class MyEntities : DbContext
{
    public MyEntities(string database) : base(database) { }
    /* Properties code omitted. */
}

2. 瞬时情况的方法如下:

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的构造者。 我还在方法中具体规定了我想要的移徙类型(此处为<代码>)。 MigrateDatabaseTo LatestVersion。

移民法典如下:

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.

移民确实改变了栏目<代码>Accounts in Months从int改为long

这是一种移民错误,因为跟踪线索需要它。 此时,我只能认为,这个问题是允许的,因为问题已经存在。 其他可能性是连接线上的一些问题。 没有人会这样做? 提前感谢!

PS:如果不清楚,我可以就这一问题提供更多信息。

问题回答

我谈几个问题。 解决办法是:

(1) 担任以下职务:

public sealed class Configuration : DbMigrationsConfiguration<YourContextClassHere>

2) 将你的种子(种子)方法作为公众

2) 在以下任何地方加入法典,这将适用最新的移徙,并更新你的b:

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());

在“视觉”和“组合”管理人Console之外开发一个数据库,使用附属migrate.exe

您可使用包装管理人配置<代码>Update-Database。 它有一条开关,具体指明连接线——要么是连接线本身($)。 链接和<代码>。LinkStringName

或您可使用<代码>DbMigrator的类别处理其编码(类似于实际migrate.exe的做法)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!