如何跨上下文共享表?
How do I share tables accross Contexts?
请参阅下面的代码,这是我从在线 CQRS 教程中借用的:
public class OrderReadContext: DbContext
{
public OrderReadContext() : base("name=GeekStuffSales") {
}
public DbSet<SalesOrder> Orders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.HasDefaultSchema("Order");
}
}
public class OrderSystemContextConfig : DbConfiguration
{
public OrderSystemContextConfig() {
SetDatabaseInitializer(new NullDatabaseInitializer<OrderReadContext>());
}
}
和:
public class OrderWriteContext : DbContext
{
public OrderWriteContext() : base("name=GeekStuffSales")
{
}
public DbSet<SalesOrder> Orders { get; set; }
public DbSet<LineItem> LineItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("Order");
modelBuilder.Entity<SalesOrder>().Ignore(s => s.LineItems);
}
}
public class OrderSystemContextConfig : DbConfiguration
{
public OrderSystemContextConfig()
{
SetDatabaseInitializer(new NullDatabaseInitializer<OrderWriteContext>());
}
}
如果我首先使用 OrderReadContext,例如通过使用 GetByID,然后 OrderWriteContext 第一次使用时出现错误:"Sales Order already exists"。如果我首先使用 OrderWriteContext,例如通过使用 InsertSalesOrder,然后第一次使用 OrderReadContext 时出现错误:"Sales Order already exists".
我明白这里发生了什么,即你不能创建相同的 table 两次。我只想创建一个数据库 table 供这两种情况使用。我该怎么做?
运行 两种上下文分别进行迁移(和更新数据库)。以下是如何处理多个上下文的示例:http://www.dotnettricks.com/learn/entityframework/entity-framework-6-code-first-migrations-with-multiple-data-contexts
鉴于这个问题被标记为 CQRS 和域驱动设计,我将从这个角度回答。在典型的 CQRS 应用程序中,读取和写入数据库是分开的。即使它们驻留在同一个数据库实例中,表也会是分开的。这是因为读写端模式可能不同,并且会在应用程序的生命周期内发生变化。
如果是这种情况,您将完全回避这个问题。
如果您觉得它有用,此图和解释为典型的 CQRS 应用程序提供了一个合理的概念架构。 CQRS + Event Sourcing – A Step by Step Overview
希望你觉得这有用。
请参阅下面的代码,这是我从在线 CQRS 教程中借用的:
public class OrderReadContext: DbContext
{
public OrderReadContext() : base("name=GeekStuffSales") {
}
public DbSet<SalesOrder> Orders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.HasDefaultSchema("Order");
}
}
public class OrderSystemContextConfig : DbConfiguration
{
public OrderSystemContextConfig() {
SetDatabaseInitializer(new NullDatabaseInitializer<OrderReadContext>());
}
}
和:
public class OrderWriteContext : DbContext
{
public OrderWriteContext() : base("name=GeekStuffSales")
{
}
public DbSet<SalesOrder> Orders { get; set; }
public DbSet<LineItem> LineItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("Order");
modelBuilder.Entity<SalesOrder>().Ignore(s => s.LineItems);
}
}
public class OrderSystemContextConfig : DbConfiguration
{
public OrderSystemContextConfig()
{
SetDatabaseInitializer(new NullDatabaseInitializer<OrderWriteContext>());
}
}
如果我首先使用 OrderReadContext,例如通过使用 GetByID,然后 OrderWriteContext 第一次使用时出现错误:"Sales Order already exists"。如果我首先使用 OrderWriteContext,例如通过使用 InsertSalesOrder,然后第一次使用 OrderReadContext 时出现错误:"Sales Order already exists".
我明白这里发生了什么,即你不能创建相同的 table 两次。我只想创建一个数据库 table 供这两种情况使用。我该怎么做?
运行 两种上下文分别进行迁移(和更新数据库)。以下是如何处理多个上下文的示例:http://www.dotnettricks.com/learn/entityframework/entity-framework-6-code-first-migrations-with-multiple-data-contexts
鉴于这个问题被标记为 CQRS 和域驱动设计,我将从这个角度回答。在典型的 CQRS 应用程序中,读取和写入数据库是分开的。即使它们驻留在同一个数据库实例中,表也会是分开的。这是因为读写端模式可能不同,并且会在应用程序的生命周期内发生变化。
如果是这种情况,您将完全回避这个问题。
如果您觉得它有用,此图和解释为典型的 CQRS 应用程序提供了一个合理的概念架构。 CQRS + Event Sourcing – A Step by Step Overview
希望你觉得这有用。