locked
Is it logical to connect to an MsSQL db without connection string and startup AddService RRS feed

  • Question

  • User283528319 posted

    Hi all

    in my project there are 2 dbcontexts

    One is used by everyone the other is used by only several users.

    Therefore I have performance concerns if I add second dbcontext with the line of code below to the startup

    services.AddDbContextPool<VNTSClientContext>((serviceProvider, optionsBuilder) =>
    {
    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("VNTSClientConnection"));

    });

    What do you think?

    Should I call that db when I ever I need instead of startup?

    Friday, July 26, 2019 9:30 AM

All replies

  • User475983607 posted

    Therefore I have performance concerns if I add second dbcontext with the line of code below to the startup

    And what is the performance concern?

    Should I call that db when I ever I need instead of startup?

    The code snippet is service configuration that runs when the application starts. 

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.adddbcontextpool?view=efcore-2.1

    Friday, July 26, 2019 10:18 AM
  • User283528319 posted

    but as far as I see when it starts it also trigger table mapping and that makes the openning time of my web site 10 sec! if table number doubles, that means opening time also doubles.

    Friday, July 26, 2019 10:56 AM
  • User1120430333 posted

    You could pass the connectionstring gotten at Startup.cs and do a new connection at the time it is needed, by using IOption.

    using System.Collections.Generic;
    using System.Linq;
    using DAL.Models.DB;
    using Entities;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    
    namespace DAL
    {
        public class DaoCache : IDaoCache
        {
            private readonly IOptions<ConnectionStrings> _options;
          
            public DaoCache(IOptions<ConnectionStrings> options)
            {
                _options = options;
            }
    
            public DtoCache GetCache()
            {
                var dtocache = new DtoCache
                {
                    ProjectTypes = new List<DtoProjectType>(),
                    Statuses = new List<DtoStatus>(),
                    Resources = new List<DtoResource>(),
                    Durations = new List<DtoDuration>()
                };
    
                using (var context = new ProjectManagementContext(_options))
                {
                    var projectypes = (from a in context.ProjectTypes select a).ToList();
                    CreateProjectTypes(dtocache, projectypes);
    
                    var statuses = (from a in context.Statuses select a).ToList();
                    CreateStatuses(dtocache, statuses);
    
                    var resources = (from a in context.Resources select a).ToList();
                    CreateResources(dtocache, resources);
    
                    var durations = (from a in context.Durations select a).ToList();
                    CreateDurations(dtocache, durations);
                }
    
                return dtocache;
            }
    
               }
    }
    
    using Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Options;
    
    namespace DAL.Models.DB
    {
        public partial class ProjectManagementContext : DbContext
        {
            private readonly IOptions<ConnectionStrings> _options;
            public ProjectManagementContext(IOptions<ConnectionStrings> options)
            {
                _options = options;
            }
    
            public ProjectManagementContext(DbContextOptions<ProjectManagementContext> options)
                : base(options)
            {
            }
    
            public virtual DbSet<Projects> Projects { get; set; }
            public virtual DbSet<Tasks> Tasks { get; set; }
            public virtual DbSet<ProjectTypes> ProjectTypes { get; set; }
            public virtual DbSet<Durations> Durations { get; set; }
            public virtual DbSet<Resources> Resources { get; set; }
            public virtual DbSet<Statuses> Statuses { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer(new AppConfiguration(_options).GetProjectMgmntConn());
                }
            }
    
            
        }
    }
    

    Saturday, July 27, 2019 6:10 AM