使用多个上下文发布

时间:2014-04-02 03:34:53

标签: entity-framework azure dbcontext ef-migrations

我正在尝试发布一个使用两个数据库连接字符串和迁移上下文的项目:

DefaultConnection - 在启动项目时自动创建并包含用户表

AmscanContext - 使用现有数据库中的Code First创建实体模型时生成

我使用不同的文件夹启用并添加了两个迁移,并更新了数据库(注释掉了我导入的数据库的创建表)

一切都在本地很好用,我甚至为控制器添加了一些身份验证和canEdit规则。

我已经设置了连接字符串来创建两个新数据库,并在下面的注释中提到。以下是每次迁移的配置。

这是应用数据:

namespace AMScan.Migrations.AmscanContext
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<AMScan.Models.AmscanContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\AmscanContext";
    }

    protected override void Seed(AMScan.Models.AmscanContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

}

这是用户数据:

namespace AMScan.Migrations.ApplicationDbContext
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
internal sealed class Configuration :      DbMigrationsConfiguration<AMScan.Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\ApplicationDbContext";
    }

    protected override void Seed(AMScan.Models.ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

}

不确定这是否会告诉你。如果您希望我发布任何特定文件,请告诉我们。

1 个答案:

答案 0 :(得分:1)

我设法让这个工作。我采取了以下步骤:

  1. 使用“在退出数据库上的Code First”创建新项目并添加数据实体模型。
  2. 创建控制器项
  3. 使用-MigrationsDirectory开关为每个上下文启用迁移(有关详细信息,请参阅here)。
  4. 添加每个迁移并更新数据库。
  5. 将解决方案发布到azure创建一个新网站和两个新数据库,确保检查两者的执行代码优先迁移。
  6. 单击注册并创建一个帐户(这将创建IdentityModel用户数据库
  7. 查看您创建的控制器的索引(这创建了应用程序数据库)
  8. 此视频对了解using code first on existing databases

    非常有用

    我希望任何寻求帮助的人都能理解这一点。我是新手,并感谢这篇文章和我读过的另一篇文章提供的帮助。

    干杯, 凯文。

相关问题