{"id":2211,"date":"2019-06-18T10:41:38","date_gmt":"2019-06-18T09:41:38","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2211"},"modified":"2022-07-25T08:33:26","modified_gmt":"2022-07-25T07:33:26","slug":"c-ef-core-connection-pooling","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-ef-core-connection-pooling\/","title":{"rendered":"C# EF core Connection Pooling"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Design;\nusing Microsoft.EntityFrameworkCore.Internal;\n\nnamespace EfDataAdapter\n{\n    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory&lt;ApplicationDbContext>\n    {\n        public static DbContextOptions Options = GetOptions();\n\n        static ApplicationDbContextFactory() { \n            DbContextPool = new DbContextPool&lt;ApplicationDbContext>(GetOptions());\n        }\n\n        private static DbContextPool&lt;ApplicationDbContext> DbContextPool { get; }\n        public static DbContextOptions GetOptions()\n        {\n            var assemblyName = typeof(ApplicationDbContextFactory).Assembly.GetName().Name;\n            var settings = new EfSettings();\n            var connectionString = settings.ConnectionString;\n            var optionsBuilder = new DbContextOptionsBuilder&lt;ApplicationDbContext>();\n            optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly(assemblyName));\n            return optionsBuilder.Options;\n        }\n        \n        public ApplicationDbContext CreateDbContext(string[] args)\n        {\n            return DbContextPool.Rent();\n        }\n        public void ReturnDbContext(ApplicationDbContext applicationDbContext)\n        {\n            DbContextPool.Return(applicationDbContext);\n        }\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Threading.Tasks;\nusing Microsoft.EntityFrameworkCore;\n\nnamespace EfDataAdapter.UnitOfWork\n{\n    public class UnitOfWork : IUnitOfWork, IDisposable\n    {\n        private ApplicationDbContext _dbContext { get; set; }\n        private ApplicationDbContextFactory _dbContextFactory;\n        public UnitOfWork(DbContext context)\n        {\n            _dbContextFactory = new ApplicationDbContextFactory();\n            _dbContext = (ApplicationDbContext)context;\n        }\n        public UnitOfWork()\n        {\n            _dbContextFactory = new ApplicationDbContextFactory();\n            CreateDbContext();\n        }\n        private void CreateDbContext()\n        {\n            _dbContext = _dbContextFactory.GetDbContext();\n        }\n        public void Commit()\n        {\n            _dbContext.SaveChanges();\n        }\n        public async Task&lt;int> CommitAsync()\n        {\n            return await _dbContext.SaveChangesAsync();\n        }\n        public DbContext GetContext()\n        {\n            return _dbContext;\n        }\n        public void Dispose()\n        {\n            Dispose(true);\n            GC.SuppressFinalize(this);\n        }\n        protected virtual void Dispose(bool disposing)\n        {\n            if (disposing)\n            {\n                if (_dbContext != null)\n                {\n                    \/\/_dbContext.Dispose();\n                    _dbContextFactory.ReturnDbContext(_dbContext);\n                }\n            }\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2211","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2211"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2226,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2211\/revisions\/2226"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}