在 ASP.Net MVC 中合并多个数据库上下文
Merging multiple database contexts in ASP.Net MVC
背景:
我有多个 类 称为 ApplicationDbContext。一个位于根目录中,其余的在它们的模块化区域目录中划分。目前,我可以选择像这样处理数据迁移。
问题:
我如何重写区域文件夹子目录中的每个 类 以消除单个迁移调用?
来源:
下面的数据库迁移命令
为根模型建立迁移
Enable-Migrations -ContextTypeName:JosephMCasey.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Root
Add-Migration -configuration JosephMCasey.Migrations.Root.Configuration Root
Update-Database -configuration JosephMCasey.Migrations.Root.Configuration -Verbose
为每个 'Areas' 模型建立迁移
Enable-Migrations -ContextTypeName:JosephMCasey.Areas.Article.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Article
Add-Migration -configuration JosephMCasey.Migrations.Article.Configuration Article
Update-Database -configuration JosephMCasey.Migrations.Article.Configuration -Verbose
理想化的迁移
Enable-Migrations
Add-Migration Application
Update-Database
C#类以下
Application\Models\IdentityModels.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Application\Areas\Article\Models\Article.cs
namespace JosephMCasey.Areas.Article.Models
{
public class ArticleContent
{
...
}
public class ApplicationDBContext : DbContext
{
public DbSet<ArticleContent> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
尾注
我是太懒了还是太挑剔了? 运行 单个上下文不是更有效吗?我是新手,所以我对最佳实践的理解充其量是不稳定的。
指导资源
Code First Migration in Multiple DbContext
Inheritance in Entity Framework: Table per Hierarchy
包管理器控制台命令 - get-help Enable-Migrations -detailed
& get-help Add-Migration -detailed
& get-help Update-Database -detailed
必须单独迁移每个唯一上下文。没有办法解决这个问题。如果您只想进行一次迁移,则必须将所有上下文合并为一个上下文。无论如何,就个人而言,我建议这样做。不需要多个上下文来为单个应用程序提供服务,除非其中一些上下文用于连接到现有数据库。在这种情况下,您无论如何都不需要迁移它们。
更新
I'm still not sure how I can merge all of this to create a single context.
在基本层面上,您的单一上下文只需要引用所有不同的区域模型命名空间:
using AwesomeApp.Areas.Foo.Models;
using AwesomeApp.Areas.Bar.Models;
using AwesomeApp.Areas.Baz.Models;
public class AwesomeAppContext : DbContext
{
// DbSets for each areas entities here
}
但是,最好只是重新组织您的项目。实体 classes 不必进入 "Models" 目录,也不必与使用它们的区域一起存储。就个人而言,如果我正在做一个相对简单的 MVC 应用程序,我通常会在项目根目录中保留主 "Models" 目录,用于应用程序使用的 all 实体以及单个引用每个的上下文。
对于更复杂的应用程序,我将其完全移出项目,并将所有实体和上下文放在 class 库中。然后,我针对 class 库进行迁移,并将其添加为对需要这些实体的项目的引用。
无论你做什么,虽然实体散布在不同区域之间会让维护成为一场血腥的噩梦,因为你总是需要记住哪个实体与哪个区域相关,上帝禁止甚至任何溢出,您开始在多个不同区域使用相同实体,但它只在一个区域中定义。
背景:
我有多个 类 称为 ApplicationDbContext。一个位于根目录中,其余的在它们的模块化区域目录中划分。目前,我可以选择像这样处理数据迁移。
问题:
我如何重写区域文件夹子目录中的每个 类 以消除单个迁移调用?
来源:
下面的数据库迁移命令
为根模型建立迁移
Enable-Migrations -ContextTypeName:JosephMCasey.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Root
Add-Migration -configuration JosephMCasey.Migrations.Root.Configuration Root
Update-Database -configuration JosephMCasey.Migrations.Root.Configuration -Verbose
为每个 'Areas' 模型建立迁移
Enable-Migrations -ContextTypeName:JosephMCasey.Areas.Article.Models.ApplicationDbContext -MigrationsDirectory:Migrations\Article
Add-Migration -configuration JosephMCasey.Migrations.Article.Configuration Article
Update-Database -configuration JosephMCasey.Migrations.Article.Configuration -Verbose
理想化的迁移
Enable-Migrations
Add-Migration Application
Update-Database
C#类以下
Application\Models\IdentityModels.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Application\Areas\Article\Models\Article.cs
namespace JosephMCasey.Areas.Article.Models
{
public class ArticleContent
{
...
}
public class ApplicationDBContext : DbContext
{
public DbSet<ArticleContent> Articles { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}
尾注
我是太懒了还是太挑剔了? 运行 单个上下文不是更有效吗?我是新手,所以我对最佳实践的理解充其量是不稳定的。
指导资源
Code First Migration in Multiple DbContext
Inheritance in Entity Framework: Table per Hierarchy
包管理器控制台命令 - get-help Enable-Migrations -detailed
& get-help Add-Migration -detailed
& get-help Update-Database -detailed
必须单独迁移每个唯一上下文。没有办法解决这个问题。如果您只想进行一次迁移,则必须将所有上下文合并为一个上下文。无论如何,就个人而言,我建议这样做。不需要多个上下文来为单个应用程序提供服务,除非其中一些上下文用于连接到现有数据库。在这种情况下,您无论如何都不需要迁移它们。
更新
I'm still not sure how I can merge all of this to create a single context.
在基本层面上,您的单一上下文只需要引用所有不同的区域模型命名空间:
using AwesomeApp.Areas.Foo.Models;
using AwesomeApp.Areas.Bar.Models;
using AwesomeApp.Areas.Baz.Models;
public class AwesomeAppContext : DbContext
{
// DbSets for each areas entities here
}
但是,最好只是重新组织您的项目。实体 classes 不必进入 "Models" 目录,也不必与使用它们的区域一起存储。就个人而言,如果我正在做一个相对简单的 MVC 应用程序,我通常会在项目根目录中保留主 "Models" 目录,用于应用程序使用的 all 实体以及单个引用每个的上下文。
对于更复杂的应用程序,我将其完全移出项目,并将所有实体和上下文放在 class 库中。然后,我针对 class 库进行迁移,并将其添加为对需要这些实体的项目的引用。
无论你做什么,虽然实体散布在不同区域之间会让维护成为一场血腥的噩梦,因为你总是需要记住哪个实体与哪个区域相关,上帝禁止甚至任何溢出,您开始在多个不同区域使用相同实体,但它只在一个区域中定义。