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.