MVC /实体代码 - 首先在它们之间具有引用完整性的多个上下文

时间:2022-10-05 15:23:13

I'm having some difficulty getting my two contexts that use the same database to cooperate. Here's the scenario:

我在使用同一个数据库进行合作的两个上下文时遇到了一些困难。这是场景:

In an MVC application using EF 6 Code-First, there is one database with two contexts. - The first context is the ApplicationIdentity context with a customized ApplicationUser object. - The second context is the business context, which holds a Team model:

在使用EF 6 Code-First的MVC应用程序中,有一个具有两个上下文的数据库。 - 第一个上下文是具有自定义ApplicationUser对象的ApplicationIdentity上下文。 - 第二个上下文是业务上下文,它包含一个Team模型:

 public class Team
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public ApplicationUser TeamLeader { get; set; }

    public string Name { get; set; }

    public virtual ICollection<ApplicationUser> TeamMembers { get; set; }

    public bool IsActive { get; set; }
}

Managing the migrations has been difficult, though this answer has proven extremely helpful: Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

管理迁移一直很困难,尽管这个答案已经证明非常有用:相同数据库中的多个DB上下文和EF 6中的应用程序以及代码优先迁移

The problem is that the Identity context keeps trying to create the Team table in it's migrations, and then the Business context keeps trying to create duplicate ApplicationUser records when a new team is created, populated, and saved.

问题是Identity上下文一直试图在其迁移中创建Team表,然后Business Context在创建,填充和保存新团队时不断尝试创建重复的ApplicationUser记录。

What I would like is for the following rules to be applied:

我想要的是适用以下规则:

  • The IdentityContext is responsible for creating and altering the schema of the Identity tables only. It has no knowledge of objects (aka Team) outside of it's area of responsibility.
  • IdentityContext仅负责创建和更改Identity表的模式。它不知道对象(即团队)在其责任范围之外。

  • The Business Context is responsible for referential integrity between it's objects and the IdentityObjects, but it may not edit records in the Identity tables. If a user does not exist, error, don't create.
  • 业务上下文负责其对象与IdentityObject之间的引用完整性,但它不能编辑Identity表中的记录。如果用户不存在,请输入错误,不要创建。

Does anyone have any tips on how to get these contexts to play nice with each other? I really don't want to break the referential integrity between Identity objects and business objects.

有没有人有任何关于如何使这些上下文互相玩耍的提示?我真的不想破坏Identity对象和业务对象之间的引用完整性。

1 个解决方案

#1


2  

What you're trying to do looks like "DDD Bounded Contexts".

你想做的事情看起来像“DDD Bounded Contexts”。

It's a bit long to explain how to use them, but here are some tips:

解释如何使用它们有点长,但这里有一些提示:

  • use modelBuilder.Ignore<EntityType>(); to exclude from your model related entities that are automatically added to your context
  • 使用modelBuilder.Ignore ();从模型中排除自动添加到上下文中的相关实体

  • use different classes in each model where necessary, and map them appropriately. I mean classes that map only part of the columns. Use modelBuilder to configure them
  • 必要时在每个模型中使用不同的类,并适当地映射它们。我的意思是只映射部分列的类。使用modelBuilder配置它们

  • use readonly navigation properties and readonly properties where necessary
  • 必要时使用只读导航属性和只读属性

This is a very interesting post by Julie Lerman: Data Points - Shrink EF Models with DDD Bounded Contexts

这是Julie Lerman发表的一篇非常有趣的文章:数据点 - 缩小具有DDD有界上下文的EF模型

#1


2  

What you're trying to do looks like "DDD Bounded Contexts".

你想做的事情看起来像“DDD Bounded Contexts”。

It's a bit long to explain how to use them, but here are some tips:

解释如何使用它们有点长,但这里有一些提示:

  • use modelBuilder.Ignore<EntityType>(); to exclude from your model related entities that are automatically added to your context
  • 使用modelBuilder.Ignore ();从模型中排除自动添加到上下文中的相关实体

  • use different classes in each model where necessary, and map them appropriately. I mean classes that map only part of the columns. Use modelBuilder to configure them
  • 必要时在每个模型中使用不同的类,并适当地映射它们。我的意思是只映射部分列的类。使用modelBuilder配置它们

  • use readonly navigation properties and readonly properties where necessary
  • 必要时使用只读导航属性和只读属性

This is a very interesting post by Julie Lerman: Data Points - Shrink EF Models with DDD Bounded Contexts

这是Julie Lerman发表的一篇非常有趣的文章:数据点 - 缩小具有DDD有界上下文的EF模型