我正在尝试发布一个使用两个数据库连接字符串和迁移上下文的项目:
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" }
// );
//
}
}
}
不确定这是否能告诉您很多信息。如果您希望我发布任何特定文件,请告诉我。
最佳答案
我已经成功地让这个工作了。我采取了以下步骤:
- 创建新项目并在退出数据库时使用 Code First 添加数据实体模型。
- 创建 Controller 项
- 使用 -MigrationsDirectory 开关为每个上下文启用迁移(有关详细信息,请参阅 here)。
- 添加每个迁移并更新数据库。
- 将解决方案发布到 Azure,创建一个新网站和两个新数据库,并确保选中两者的“执行代码优先迁移”。
- 点击“注册”并创建一个帐户(这将创建 IdentityModel 用户数据库
- 查看您创建的 Controller 的索引(这创建了应用数据库)
该视频对于了解 using code first on existing databases 也非常有用。
我希望这对任何寻求帮助的人来说都是有意义的。我对此很陌生,感谢这篇文章和我读过的其他文章中提供的帮助。
干杯, 凯文。
关于entity-framework - 多上下文发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801056/