Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework DBContext

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.

like image 1000
Guygar Avatar asked Dec 30 '16 14:12

Guygar


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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.

like image 133
Masoud Andalibi Avatar answered Sep 27 '22 23:09

Masoud Andalibi


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

like image 44
Z.D. Avatar answered Sep 28 '22 01:09

Z.D.