【问题标题】:Entity Splitting using inherited DBContext使用继承的 DBContext 进行实体拆分
【发布时间】:2016-06-15 20:28:00
【问题描述】:

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

创建一个知道 Core.Customer 表的“核心”DBContext。那没问题。现在我想用派生自核心 DBContext 的 DBContext 创建一个新的类库。同样,没问题。

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

提前谢谢你...

【问题讨论】:

    标签: entity-framework


    【解决方案1】:

    Lerman 女王在许多帖子中以示例描述了限界上下文概念,这对您的用例非常有用: 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

    【讨论】:

    • 感谢您的回复!我正在读这些。到目前为止,我在这两篇文章中都没有看到任何内容显示实体将如何在派生的 DbContext 中继承,但我刚刚开始浏览这些内容,因此我将继续阅读。
    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 2011-10-20
    • 2010-12-29
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多