entity-framework - 多上下文发布

标签 entity-framework azure dbcontext entity-framework-migrations

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

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

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

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

一切在本地运行良好,我什至向 Controller 添加了一些身份验证和 canEdit 规则。

我已经设置了连接字符串来创建两个新数据库,并在下面的评论中提到。这是每次迁移的configuration.cs。

这是应用程序数据:

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. 创建新项目并在退出数据库时使用 Code First 添加数据实体模型。
  2. 创建 Controller 项
  3. 使用 -MigrationsDirectory 开关为每个上下文启用迁移(有关详细信息,请参阅 here)。
  4. 添加每个迁移并更新数据库。
  5. 将解决方案发布到 Azure,创建一个新网站和两个新数据库,并确保选中两者的“执行代码优先迁移”。
  6. 点击“注册”并创建一个帐户(这将创建 IdentityModel 用户数据库
  7. 查看您创建的 Controller 的索引(这创建了应用数据库)

该视频对于了解 using code first on existing databases 也非常有用。

我希望这对任何寻求帮助的人来说都是有意义的。我对此很陌生,感谢这篇文章和我读过的其他文章中提供的帮助。

干杯, 凯文。

关于entity-framework - 多上下文发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801056/

相关文章:

c# - 用于查找具有非主键值的实体的存储库方法

entity-framework - DbContext 模型中包含什么?

azure - 尝试更改 Azure 应用服务计划时找不到应用服务计划

azure - 使用 Ansible 如何在 Azure 中配置网络接口(interface)并将其与另一个资源组中的虚拟网络关联?

azure - Graph Api 响应中的 nextLink 不是绝对 Uri

Quartz.Net 作业中使用的 DBContext 的 Ninject 作用域

C# Entity Framework 6 上下文对象数据库首先使用现有的 DbConnection

c# - 当给定可能为 null 的 int 时返回 Color.FromArgb 的最佳实践 C#

entity-framework - Entity Framework -联合会导致 "Unable to create a constant value of type.."

c# - 与硬编码整数值相比,使用整数变量时 Linq to SQL 查询非常慢