Eric Harmon
Eric Harmon

Reputation: 195

Entity Splitting using inherited DBContext

I have a database with some "Core" tables in it, which are all in the Core schema. There is an additional schema named Extra, with additional tables in it. Given a table Customer, where there is a Core.Customer table with CustomerID and CustomerName, and an Extra.Customer table in it with CustomerID and AdditionalInfo, I want to do the following:

Create a "core" DBContext which knows about the Core.Customer table. That's no problem. Now I want to create a new class library with a DBContext that derives from the core DBContext. Again, no problem.

However, I now want to be able to access a split entity (Customer) from the derived project. This is where I'm having difficulty. I will have multiple projects that use these core tables, so I need the core DBContext. For certain clients (each client will get their own schema in the database), I want to be able to "extend" the core entities using entity splitting. So far, I haven't had any luck with this. Can someone tell me if this is possible?

Thank you in advance...

Upvotes: 1

Views: 401

Answers (1)

Bassam Alugili
Bassam Alugili

Reputation: 17003

The queen Lerman, has described the Bounded Context concept with example in many posts, which is very usefull for your use case: https://msdn.microsoft.com/en-us/magazine/jj883952.aspx

Miller, has a good example for the multiple dbContexts shema: https://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/

Combine the both solutions and you will get exactly what you want :-)

Example:

public class BaseContext<TDbContext > : DbContext where TDbContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TDbContext>(null);
    }

    protected BaseContext() : base("name")
    {

    }
}


public class BoundedDbContext1: BaseContext<BoundedDbContext1>
{
    public DbSet<Domain.Users> Customers { get; set; }
}


public class BoundedDbContext2 : BaseContext<BoundedDbContext2>
{
    public DbSet<Domain.Registration> Customers { get; set; }
}

The concept behind:

http://martinfowler.com/bliki/BoundedContext.html

Upvotes: 1

Related Questions