C # Entity Framework DBContext

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

The database I will work with contains 800+ tables, and when working with certain parts of the application, I only deal with 1-10 tables.

So my question is; will not have a DBC context including 800+ classes will have a big negative impact on system resources?

I assume that I am new to this technology and am confused about the actual value of the information that I accept during my research.

Thank.

NOTE. Thanks for your details. Please take a look at this post: Using multiple DbContexts with shared storage and unit of work . It says that I cannot have tables in different contexts that are related to each other ?!

But in the real world of scenerio, my understanding is that to spread faults between table relationships in focused areas, as is done in Code First EF? Thanks again.

+4
source share
2 answers

You only need the tables you work with in your db context (if db already exists). The only reason you will need a db context with all tables is if you want to recreate the entire db from scratch.

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

+2
source

Update

, DbContext, , :

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(); 
    }
}

DbSets.


EF Code-First, POCO , , . , 800, , Database-First Approach. , , .

:

DataBase-First: Ado.NET Entity Model DbContext! .Edmx, POCO .

, Code-First Approach, , DbContext:

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());
    }
}

DbSet<Class> DbContext, :

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

, DbContext Area, MVC. , Admin AreaAdminDbContext .

+1

Source: https://habr.com/ru/post/1665334/


All Articles