Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should EF's DbContext contains all tables?

I'm new to EF4, and I'm trying to figure out the best way to create my DbContext class(es).

Is there any problem (specially performance) in putting all my tables / entities into one and only one DbContext class, like the code below?

public class AllInOneDb : DbContext
{
    public DbSet<Customer> Customers{ get; set; }
    public DbSet<Address> Addresses{ get; set; }
    public DbSet<Order> Order{ get; set; }
    public DbSet<Product> Products{ get; set; }
    public DbSet<Category> Categories{ get; set; }
    // and more and more entities...
}

Or should I model my classes based on the subsets of functionalities?

public class CustomerDb : DbContext
{
    public DbSet<Customer> Customers{ get; set; }
    public DbSet<Address> Addresses{ get; set; }
    public DbSet<Order> Order{ get; set; }
}

public class ProductDb : DbContext
{
    public DbSet<Product> Products{ get; set; }
    public DbSet<Category> Categories{ get; set; }
    public DbSet<Order> Order{ get; set; } // look Order entity again!
}

Thanks

like image 512
Fabio Avatar asked Apr 14 '12 02:04

Fabio


People also ask

Which class derives dbcontext in Entity Framework?

The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database.

What is a dbcontext and why we need it?

What is DbContext? What is the purpose of a DbContext and why we need it in our application when using Entity Framework? DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. The primary class that is responsible for interacting with data as objects DbContext.

How to create a context in Entity Framework?

The recommended way to work with context is to derive your class DbContext and expose the DbSet properties that represent collections of the specified entities in the context. If you are using the EF Designer, Entity Framework will generate the context. But if you are using the Code First approach, you will typically write the context yourself.

How to add new entity to dbcontext with added state?

This new entity data will be inserted into the database when SaveChanges () is called. Asynchronous method for adding a new entity to DbContext with Added state and starts tracking it. This new entity data will be inserted into the database when SaveChangesAsync () is called.


1 Answers

If you have sub areas which have specific business logic, you can split it into multiple DbContext. (These smaller contexts follow a pattern critical to Domain Driven Design called Bounded Contexts). There are a number of benefits to creating DbContexts that are targeted to these various processes rather than one all-purpose context. As your application grow, it will be much easier to maintain each context as well as locate the logic you need within it. (Better than adding or modifying existing logic in single DbContext with many DbSet properties and fluent configuration for many class)

Performance is another consideration. When Entity Framework creates an in-memory model of the context, the larger the context is the more resources are expended to generate and maintain that in-memory model.

If you are going to share instances(Order) between multiple contexts, Entity can only be attached to one context at a time. Detach Order from the Customer DbContext first and attach Order to a Product DbContext. And you should be careful about (or just avoid) moving Added, Modified, or Deleted entities from one context to another.

Order order;
using (var custDb = new CustomerDb()){
   order = custDb.FirstOrDefault(o=>OrderId == "orderid");
}
using (var prodDB = new ProductDb()){
   prodDB.Attach(order);
   ...
}
like image 99
Min Min Avatar answered Oct 11 '22 11:10

Min Min