[英]How to create relationships between models that are found on different DbContext?
I am developing an application following the DDD pattern. 我正在按照DDD模式开发应用程序。
I have the following contexts for employee management and user account management called 我有以下用于员工管理和用户帐户管理的上下文,称为
EmployeeManagementContext EmployeeManagementContext
and 和
UserAccountManagementContext UserAccountManagementContext
Both contexts are on a separate project. 两种上下文都在单独的项目中。
The project for employee management has the following models. 员工管理项目具有以下模型。
public class Employee
{
public int Id { get; private set; }
public string Name { get; private set; }
public DateTime DateOfBirth { get; private set; }
}
The project for user account management has the following models. 用户帐户管理项目具有以下模型。
public class Employee
{
public int Id { get; private set; }
public string Name { get; private set; }
}
public class UserAccount
{
public int Id { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
}
EmployeeManagementContext EmployeeManagementContext
public class EmployeeManagementContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
UserAccountManagementContext UserAccountManagementContext
public class UserAccountManagementContext : DbContext
{
public DbSet<UserAccount> UserAccounts { get; set; }
}
I can successfully migrate both context by having different context keys but the problem is I loose the relationship between the Employee and UserAccount models. 我可以通过使用不同的上下文键来成功迁移两个上下文,但是问题是我松开了Employee模型和UserAccount模型之间的关系。
Basically, the business rules that I need to implement between the two models are as follow: 基本上,我需要在两个模型之间实现的业务规则如下:
An Employee may or may not have a UserAccount. 员工可能拥有或可能没有UserAccount。
A UserAccount is owned by exactly one Employee only. 一个UserAccount仅由一个雇员拥有。
This means that I should have a one to zero-or-one relationship between Employee and UserAccount like the diagram below. 这意味着我应该在Employee和UserAccount之间建立一对零或一对一的关系,如下图所示。
Please disregard the wrong relationship notation, its seems to be a limitation of the tool I am using but it is a one-to-zero-or-one relationship I assure you. 请忽略错误的关系表示法,它似乎是我使用的工具的局限性,但我向您保证这是一对零或一对一的关系。
I tried the following configurations in UserAccount project: 我在UserAccount项目中尝试了以下配置:
public class UserAccountConfiguration : EntityTypeConfiguration<UserAccount>
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasOptional(x => x.UserAccount)
.WithRequired(x => x.Employee);
}
public class UserAccountManagementContext : DbContext
{
public DbSet<UserAccount> UserAccounts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserAccountConfiguration());
modelBuilder.Configurations.Add(new EmployeeConfiguration());
base.OnModelCreating(modelBuilder);
}
}
The above configurations result to an error because Employee table already exist because of the EmployeeManagementContext in employee management project. 由于雇员管理项目中的EmployeeManagementContext,上述配置导致错误,因为Employee表已经存在。
If I try to add the following configuration in EmployeeConfiguration of employee management project, 如果我尝试在员工管理项目的EmployeeConfiguration中添加以下配置,
ToTable("Users"); ToTable( “用户”);
EF doesn't complain anymore and creates a Users table which then creates the relationship I need between Employee and UserAccount. EF不再抱怨,并创建了一个Users表,然后在Employee和UserAccount之间创建了我需要的关系。 But the problem is, if I try to query Employee/Users in UserAccountManagementContext, it doesn't contain anything and I don't think its good design to keep on creating smaller tables that is derived from the orginal table because it will only clutter the database, right?... 但是问题是,如果我尝试在UserAccountManagementContext中查询Employee / Users,它不包含任何内容,而且我认为继续创建从原始表派生的较小表的良好设计也不可行,因为它只会使数据库,对吧?
I would greatly appreciate your help, thanks. 非常感谢您的帮助,谢谢。
You should focus more on the domain and less on the database. 您应该更多地关注域而不是数据库。
From what I can see, you have two Aggregates ( Employee
and UserAccount
), in possible 2 bounded contexts (I can't name them as I don't have enough data). 据我UserAccount
,您有两个聚合( Employee
和UserAccount
),可能有2个有界上下文 (由于没有足够的数据,我无法命名它们)。 In general it's not recommended to force any invariant in a strongly consistent manner between the two Aggregates but there are exceptions. 通常,不建议在两个聚合之间以强烈一致的方式强制使用任何不变式,但是有例外。 They may be as well in different databases, having different technologies. 它们可能也位于具有不同技术的不同数据库中。 Let's now see how you can enforce the two invariants: 现在让我们看看如何实现两个不变式:
An Employee may or may not have a UserAccount. 员工可能拥有或可能没有UserAccount。
This can be modeled with a nullable UserAccountId
on a Employee
, without any low level database references. 可以在Employee
上使用可为空的UserAccountId
建模,而无需任何低级数据库引用。 Depending on the business rules, when an UserAccound is deleted (if this is a valid business operation on it), using a Saga/Process manager, you can set to null
the corresponding UserAccountId
in the Employee
that had this account. 根据业务规则,使用Saga /流程管理器删除 UserAccound时(如果这是有效的业务操作),则可以将拥有此帐户的Employee
中的相应UserAccountId
设置为null
。
A UserAccount is owned by exactly one Employee only. 一个UserAccount仅由一个雇员拥有。
The simplest way to enforce this invariant is of technological nature: create an unique index on the UserAccountId
. 强制执行此不变式的最简单方法是技术性的:在UserAccountId
上创建唯一索引。 Other solutions imply using Sagas but are not as good as this one, for example would permit for a short period of time for the invariant to be broken. 其他解决方案暗示使用Sagas,但不如Sagas好,例如,允许在短时间内破坏不变式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.