Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Create database context from cookie and base path

$
0
0

Postgres database has multiple schemes like  company1, company2, ... companyN

Browser sends cookie containing scheme name . Data access operations should occur in this scheme. Web application user can select different scheme. In this case different cookie value is set.

Npgsql EF Core Data provider is used.

ASP NET MVC 5 Core application registers factory in StartUp.cs :

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddScoped<IEevaContextFactory, EevaContextFactory>();
          ....

Home controller tries to use it:

    public class HomeController : EevaController
    {
        public ActionResult Index()
        {
            var sm = new SchemeManager();
            sm.PerformInsert();
        ....

This throws exception since factory member is null. How to fix this ?

    public interface IEevaContextFactory    {        EevaContext Create();    }    public class EevaContextFactory : IEevaContextFactory    {        private IHttpContextAccessor httpContextAccessor;        private IConfiguration configuration;        public EevaContextFactory(IHttpContextAccessor httpContextAccessor, IConfiguration configuration)        {            this.httpContextAccessor = httpContextAccessor;            this.configuration = configuration;        }        public EevaContext Create()        {            var builder = new DbContextOptionsBuilder<EevaContext>();            var pathbase = httpContextAccessor.HttpContext.Request.PathBase.Value;            var scheme = httpContextAccessor.HttpContext.Request.Cookies["Scheme"];            var csb = new NpgsqlConnectionStringBuilder()            {                Host = pathbase,                SearchPath = scheme            };            builder.UseNpgsql(csb.ConnectionString);            return new EevaContext(builder.Options);        }    }


Scheme data acess methods:


    public class SchemeManager    {        readonly IEevaContextFactory factory;        public SchemeManager(IEevaContextFactory factory)        {            this.factory = factory;        }        public SchemeManager()        {        }        public void PerformInsert()        {            using (var context = factory.Create())            {                var commandText = "INSERT into maksetin(maksetin) VALUES (CategoryName)";                context.Database.ExecuteSqlRaw(commandText);            }        }    }


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>