与同一数据库内的多个迁移和传承,从基地DbContexts实体框架 [英] Entity Framework with multiple migrations and inheritance from base DbContexts within the same Database

查看:139
本文介绍了与同一数据库内的多个迁移和传承,从基地DbContexts实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个可扩展的定制CMS解决方案,其中我有一个名为CmsDbContext与实体CMS表,如父的DbContext:用户表。 CmsDbContext启用了自动迁移。
欲允许最终用户从CmsDbContext继承以创建自定义额外的表/实体,通过添加/映射DbSet属性和定制POCO的



在CMS需要他的CmsDbContext获得预启动初始化,派生的DbContext的初始化之前。
到目前为止好,当我初始化CmsDbContext的表成功创建。
当我初始化派生的DbContext如CustomCmsDbContext,成功创建了额外的表。但在重新启动Web应用程序时,CmsDbContext的迁移过程擦除不是由上下文所拥有的表,这些表为继承CustomDbContext被重新创建



我知道,它可以创建在同一个数据库中启用了自动迁移多个不相关DbContexts,因为DbMigrationsConfiguration会的ContextKey财产。但是,我在这里需要继承,以允许CustomCmsDbContext方面做出反对CmsDbContext映射现有表的查询,所以这两种情况下的行为诸如One的DbContext,具有迁移功能。



所以我们知道问题是父的DbContext在迁移过程中删除子的DbContext表,而孩子的DbContext在迁移过程中重新创建缺少的表。



如何避免迁移?删除未通过的DbContext拥有的表



下面是伪代码:

 公共类CmsDbContext:{的DbContext 

静态CmdDbContext(){
Database.SetInitializer(新MigrateDatabaseToLatestVersion< CmsDbContext,Migrations.CmsDbContextConfiguration>());
}

公共DbSet<使用者>用户{搞定;组; }
}


公共类CustomCmdDbContext:CmsDbContext {

静态CustomCmdDbContext(){
Database.SetInitializer(新MigrateDatabaseToLatestVersion< CustomCmdDbContext, Migrations.CustomCmdDbContext>());
}
公共DbSet< CustomEntity> CustomEntities {搞定;组; }
}


解决方案

这从罗恩·米勒博客文章 descripes很类似的方案:让客户添加/编辑用户definied上下文列或表


I'm building an extensible custom CMS solution where I have a parent DbContext named CmsDbContext with the entities for CMS tables, eg: Users table. CmsDbContext has automatic migrations enabled. I want allow the final user inherit from CmsDbContext in order to create custom extra tables/entities, by adding/mapping DbSet properties and custom POCO's.

The Cms needs that his CmsDbContext get initialized in PreStart, before the initialization of derived DbContext. So far so good, when I initialize CmsDbContext the tables are created sucessfully. When I initialize the derived DbContext e.g. CustomCmsDbContext, the extra tables are created successfully. But when the Web application is restarted, the migration process of CmsDbContext erase the tables that isn't owned by the context, and the tables for the inherited CustomDbContext are recreated.

I Know that it's possible to create multiple not related DbContexts with automatic migrations enabled within the same database, because of the ContextKey property of DbMigrationsConfiguration will. But I need inheritance here in order to allow the CustomCmsDbContext context make queries against existing tables mapped by CmsDbContext, so both contexts behaves like one DbContext, with migrations enabled.

So we know the problem is that the parent DbContext deletes tables of the child DbContext during migration process, and the child DbContext recreates the missing tables during migration process.

How to avoid migrations from deleting tables that are not owned by DbContext ?

Here is the pseudo code:

public class CmsDbContext: DbContext {

    static CmdDbContext() {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<CmsDbContext, Migrations.CmsDbContextConfiguration>());
    }

    public DbSet<User> Users { get; set; }
}


public class CustomCmdDbContext: CmsDbContext {

    static CustomCmdDbContext() {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<CustomCmdDbContext, Migrations.CustomCmdDbContext>());
    }
    public DbSet<CustomEntity> CustomEntities { get; set; }
}

解决方案

This blog post from Rowan Miller descripes a very similar scenario: Let the customer add/edit user definied columns or tables to the context.

这篇关于与同一数据库内的多个迁移和传承,从基地DbContexts实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆