0

In the MVC Music Store (http://www.asp.net/mvc/tutorials/mvc-music-store), the author has created a class called MVCMusicStoreEntities which looks like this:

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
    public DbSet<Album> Albums { get; set; }
    public DbSet<Genre> Genres { get; set; }
    public DbSet<Artist> Artists { get; set; }
    public DbSet<Cart> Carts { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    }
}

I can see why this has been done, it gives the models easy access to all the database data via Entity Framework.... this feels a bit "dirty" to me.

Is this correct practice, to put all your DbSet's into a class which inherits from the DbContext class?

I'm writing something by self, where I will need to use:

public virtual DbSet<User> Users { get; set; }

and

public virtual DbSet<Category> Categories { get; set; }

There are cases when I will only want a model to use Users and not Categories, so wrapping them both in a class like a waste.

I could wrap DbSet<User> in a class and wrap DbSet<Category> in a separate class, but Entity Framework also wants to use the wrapper class's name as the connection string name... so if I wanted multiple classes inheriting from DbContext, I'll need multiple connection strings? Is this bad practice? Is there a better way?

Can I use DbContext on the fly within a method? Rather than creating a wrapper class which inherits from DbContext.

3
  • 1
    Why does it feel dirty to you? If you really wanted to, you could only expose the entities you feel people should bet querying, but you would still be able to get to those other entities regardless because DbContext exposes the Set<T>() method, which would give you access to a DbSet<T>. You could also create what is known as a bounded context, where you separate your contexts by domain. It is no more wrong or right than the Music Store example. It all depends on your specific needs. Commented May 20, 2014 at 12:28
  • I would say it's a rather complex topic. Please watch Julie Lerman's presentation talking about DDD and it covers your question. Should we put all DBsets in one giant context object? Here's the link: dddcommunity.org/ddd-contributors/… Commented May 20, 2014 at 12:50
  • Larry, can you make your comment into an answer? That like was exactly what I was looking for! Commented May 20, 2014 at 16:52

5 Answers 5

2

My suggestion is to not worry about this. Add all DbSets to your DbContext and use only the ones that you need. This will take less time to implement. And if you worry about performance, difference will be unnoticeable anyway. Entity Framework only loads the data when you query it.

Sign up to request clarification or add additional context in comments.

Comments

1

In Entity Framework: 1 DbContext = 1 Database. Your DbContext is your Unit of Work, and each DbSet is a repository. DbContext is only intended to be a one-stop shop to everything that your application (your entire application) will need to access from a particular database.

If you want to do things like limit access to certain datasets to only certain areas of your application, that is not a task for DbContext. Instead, it's a task for a custom service class that will internally work with your DbContext, but only expose certain endpoints.

3 Comments

EF6 supports "* DbContext = 1 Database" scenarios to prevent very large contexts. But yes, he should start with one and get familiar with that before optimizing.
True. I actually never understood why that was such a requested feature, though. You could achieve multiple contexts for a single-database even before EF6, though not trivially, but even then, I never found a practical application for it. Generally, all data in the same database is so closely related, that it's impractical to truly separate it into logical chunks that won't need to operate with each other. And, if you can do that, I'd say that may be an argument for creating another database, instead.
Multiple contexts make sense when there are multiple domains in a same DB (i.e. accounting and inventory in same DB, different modules/contexts). If the domains share (hopefully few) tables/entities EF6 supports this as well (i.e. same reference entity in multiple contexts).
0

Good Question. You just start to walking in the road of Enterprise Patterns, In this scenario you need Repository and Unit of Work Patterns :

The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store

Take a look at this basic sample : http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

good luck.

1 Comment

Entity Framework already implements the Repository and Unit of Work patterns. By implementing it again on top of Entity Framework, you're just making your application harder to maintain.
0

Even if you weren't using a wrapping class, in your dbContext you could address your table as this.Set<EntityType>() (returns your DbSet<EntityType>). So in essence, your wrapping class is nothing else but shorthand for the default method.

You could even make them into automated properties, if you wanted:

public DbSet<Album> Albums { get { return this.Set<Album>(); }

Comments

0

Also note that if you do choose to have multiple DbContexts you don't really need multiple connection strings. You could just make your contexts re-use the same connection string since you can specify its name explicitly in DbContext's constructor:

public class UsersDbContext: DbContext
{
    public UsersDbContext(): base("name=ConnectionStringName") { }
    ...
}

public class CategoriesDbContext: DbContext
{
    public CategoriesDbContext(): base("name=ConnectionStringName") { }
    ...
}

There are two forms available: base("ConnectionStringName") or base("name=ConnectionStringName"). More details here: http://msdn.microsoft.com/en-us/data/jj592674.

One other thing to consider is that using migrations gets harder when you use multiple contexts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.