MVC / Entity Code - the first few contexts with referential integrity between them

I am having difficulty getting two of my contexts that use the same database for collaboration. Here's the script:

In an MVC application using EF 6 Code-First, there is one database with two contexts. - The first context is the ApplicationIdentity context with the ApplicationUser custom object. - The second context is a business context that contains the team model:

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 migrations was difficult, although this answer turned out to be extremely useful: Several database contexts in the same database and application in EF 6 and the first code migrations

The problem is that the Identity context tries to create a Team table in it, and then the Business context continues to duplicate ApplicationUser records when creating, populating and saving a new team.

I would like to use the following rules:

  • IdentityContext is responsible for creating and modifying the schema of only Identity tables. He does not know about objects (aka Team) outside the scope of responsibility.
  • The business context is responsible for referential integrity between objects and IdentityObjects, but may not edit records in Identity tables. If the user does not exist, no error is generated.

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

+6
source share
1 answer

What you are trying to do looks like "Limited DDD Contexts".

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

  • use modelBuilder.Ignore<EntityType>(); to exclude from model-related objects that are automatically added to your context.
  • If necessary, use different classes in each model and map them accordingly. I mean classes that display only part of the columns. Use modelBuilder to configure them.
  • use read navigation properties and readonly properties where necessary

This is a very interesting article by Julia Lerman: Data Points - EF Shrink Models with Limited DDD Context

+2
source

All Articles