实体拆分使用继承的DBContext

时间:2016-06-15 11:55:43

标签: entity-framework

我有一个数据库,其中有一些" Core"其中的表,都在Core架构中。还有一个名为Extra的附加模式,其中包含其他表。给定一个表Customer,其中有一个带有CustomerID和CustomerName的Core.Customer表,以及一个带有CustomerID和AdditionalInfo的Extra.Customer表,我想要执行以下操作:

创建一个"核心"知道Core.Customer表的DBContext。这没问题。现在我想创建一个带有DBContext的新类库,该DBContext派生自核心DBContext。再一次,没问题。

但是,我现在希望能够从派生项目访问拆分实体(Customer)。这是我遇到困难的地方。我将有多个使用这些核心表的项目,所以我需要核心DBContext。对于某些客户端(每个客户端将在数据库中获得自己的架构),我希望能够扩展"使用实体拆分的核心实体。到目前为止,我还没有幸运。有人可以告诉我这是否可能?

提前谢谢你......

1 个答案:

答案 0 :(得分:0)

女王Lerman在许多帖子中用例子描述了Bounded Context概念,这对你的用例非常有用: https://msdn.microsoft.com/en-us/magazine/jj883952.aspx

Miller,有多个dbContexts shema的好例子: https://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/

结合这两种解决方案,您将得到您想要的: - )

示例:

public class BaseContext<TDbContext > : DbContext where TDbContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TDbContext>(null);
    }

    protected BaseContext() : base("name")
    {

    }
}


public class BoundedDbContext1: BaseContext<BoundedDbContext1>
{
    public DbSet<Domain.Users> Customers { get; set; }
}


public class BoundedDbContext2 : BaseContext<BoundedDbContext2>
{
    public DbSet<Domain.Registration> Customers { get; set; }
}

背后的概念:

http://martinfowler.com/bliki/BoundedContext.html

相关问题