Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC / Entity Code-First Multiple Contexts with Referential Integrity between them

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:

 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

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.

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.
  • 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.

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.

like image 633
Matt Studley Avatar asked Nov 26 '15 01:11

Matt Studley


People also ask

How to define multiple dbcontext classes in Entity Framework?

Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases. In our example, we will define two Context classes for the same database. In the following code, there are two DbContext classes for Student and Teacher.

How to create an MVC web app with Entity Framework?

In this tutorial, you: 1 Create an MVC web app 2 Set up the site style 3 Install Entity Framework 6 4 Create the data model 5 Create the database context 6 Initialize DB with test data 7 Set up EF 6 to use LocalDB 8 Create controller and views 9 View the database More ...

How do I create a database context in Entity Framework?

Create the database context. The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code, you specify which entities are included in the data model.

What is a referential integrity constraint violation?

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. I've taken a really careful look at the entity which looks normal.


1 Answers

What you're trying to do looks like "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
  • 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
  • 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

like image 75
JotaBe Avatar answered Oct 13 '22 05:10

JotaBe