7

I am trying to learn Code First EF6 and I am confused regarding DBContext.

The database I will be working on contains 800+ tables, while when working of specific parts of the application I am only dealing with 1-10 tables.

So my question is; would not having a DBContext involving 800+ Classes have a big negative impact on system resources?

I guess I am new to this technology and confused regarding the actual meaning of the information that I am taking in during my research.

Thank you.

NOTE: Thank you for your inputs. Please take a look at this post: Using multiple DbContexts with a generic repository and unit of work. There it states I cannot have tables in separate contexts that relate to each other?!

But in a real world scenerio my understanding is that it is common to break up the table relationships in focused areas, how is this done in Code First EF? Thanks again.

Community
  • 1
  • 1
Guygar
  • 449
  • 1
  • 5
  • 8
  • Just use DB first approach and import the entities that are required. – Ric Dec 30 '16 at 14:44
  • 1
    you dont want one model with 800+ table objects. You can have multiple dbcontext models. You can create one model for dealing with staff, and another for dealing with courses, or whatever other areas your project deals with – JamieD77 Dec 30 '16 at 15:01
  • Updated my answer – Masoud Andalibi Dec 30 '16 at 17:01

2 Answers2

5

Updated

If you are using Repository Pattern, you cannot go make multiple DbContext, You Create One Generic, And pass it to your Generic Repository like below :

public class Repository<T> : IRepository<T>
    where T : EntityBase
{
    internal MyDbContext context;
    internal DbSet<T> dbSet; 

    public Repository()
    {
        context = new MyDbContext();
        this.dbSet = context.Set<T>(); 
    }
    public void Add(T entity)
    {
        dbSet.Add(entity);
    }
    public void Delete(T entity)
    {
        dbSet.Remove(entity);
    }
    public void Delete(int id)
    {
        dbSet.Remove(dbSet.Find(id));
    }
    public T GetById(int id)
    {
        return dbSet.Find(id);
    }
    public IEnumerable<T> GetAll()
    {
        return dbSet.AsEnumerable();
    }
    public void Update(T entity)
    {
        dbSet.Attach(entity);
        context.Entry(entity).State = EntityState.Modified;
    }
    public void Save()
    {
        context.SaveChanges(); 
    }
}

And you should include your DbSets there as well.


If you are doing EF Code-First then its yours to design your POCO class based on how many are needed but no more. But based on what you said about 800 tables i think you may want to try Database-First Approach rather. i suggest you this article very carefully as it explain everything you need.

Update:

If you Approach this from DataBase-First: Ado.NET Entity Model Creates your DbContext for you! If you take a closer look at the .Edmx file it is basically your POCO Classes within.

Now if you try Code-First Approach, lets say you have this DbContext Class:

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("name=MyConnection")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, YourApplication.Migrations.Configuration>("MyConnection")); 
    }
    //Every time you need to add new Table you add them here.
    public DbSet<Users> Users { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //And Map them here
        modelBuilder.Configurations.Add(new UsersMap());
    }
}

You just add a new DbSet<Class> to your DbContext like below:

    public DbSet<POCO CLASS> CLASS { get; set; }

And so on, i normally create a DbContext for Every Area i have in my MVC Application. So you can go like Admin Area -> AdminDbContext And so on.

Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44
  • What I am finding confusing is; We have a container app and within it there are many components (Client/Staff records, Courses, Business calender, Golf...), so how do I develop the application from this point of view i.e. do we keep adding new Classes to DBContext as our need grows? Thanks. – Guygar Dec 30 '16 at 14:53
  • @Guygar let me give you and example then. check back in afew mins – Masoud Andalibi Dec 30 '16 at 14:54
4

You only need the tables you are working with in your db context (if the db already exists). The only reason you'd need a db context with all the tables would be if you want to recreate the whole db from scratch.

Take a look at the bounded context pattern from DDD: http://martinfowler.com/bliki/BoundedContext.html

Z .
  • 12,657
  • 1
  • 31
  • 56
  • Great link! Focuses on what my question is all about. Could you kindly point me to the right direction regarding its implementation in a Code-First Entity Framework environment? Thanks. – Guygar Dec 31 '16 at 18:40
  • 1
    https://msdn.microsoft.com/en-us/magazine/jj883952.aspx?f=255&MSPPError=-2147217396 – Z . Dec 31 '16 at 21:02