.NET

    Multiple EF Core DbContexts in a Single Query Execution    


    Using multiple DB context in Entity Framework core

    Working with two or more DbContexts in Entity Framework Core is beneficial when an element of an application has different databases or when different models are used in an application. This is especially advantageous in multi modular systems where each module or a particular feature has different database needs.

    Why Use Multiple DbContexts?

    1. Separation of Concerns: It is possible to have different sub parts in the application (for example, the user management, ordering or logging parts) and each of them may have its own model that offers data and business control.
    2. Improved Performance: This way of merging the read and write operations presupposes their differentiation that can minimize the problem of contention and maximize the results obtained.
    3. Modular Design: Every DbContext correlates with the bounded context thus application, testing and maintenance of modules are implemented easily.
    4. Legacy Integration: If you are going to work with legacy systems and you have your own database schemas you need to deal with, separate DbContexts can make this easier on you.

    Configuration and how to use multiple DB Context

    Here is a basic procedure on how to use this strategy effectively:

    1. Define Your DbContexts

    In this approach, different types of DbContexts shall be created in different classes.

    public class AppDbContext : DbContext{ public DbSet<User> Users { get; set; } public DbSet<Order> Orders { get; set; } public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }}public class AuditDbContext : DbContext{ public DbSet<AuditLog> AuditLogs { get; set; } public AuditDbContext(DbContextOptions<AuditDbContext> options) : base(options) { }}

     

    2. Configure in Startup.cs or Program.cs file

    In the AddDbContext method connect each DbContext to its own particular connection string.

    services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AppConnection")));services.AddDbContext<AuditDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AuditConnection")));

     

    3. Use Multiple Contexts in Services

    Inject required DbContext into services.

    public class UserService{ private readonly AppDbContext _appDbContext; private readonly AuditDbContext _auditDbContext; public UserService(AppDbContext appDbContext, AuditDbContext auditDbContext) { _appDbContext = appDbContext; _auditDbContext = auditDbContext; } public async Task CreateUser(User user) { _appDbContext.Users.Add(user); await _appDbContext.SaveChangesAsync(); var auditLog = new AuditLog { Action = "CreateUser", Timestamp = DateTime.UtcNow }; _auditDbContext.AuditLogs.Add(auditLog); await _auditDbContext.SaveChangesAsync(); }} 

    Ready to transform your business with our technology solutions?   Contact Us today to Leverage Our .Net Expertise.

    Share

    facebook
    LinkedIn
    Twitter
    Mail
    .Net

    Related Center Of Excellence