none
How to use EF sharing same DbContext for multiple databases RRS feed

  • Question

  • I am having a multitenant application which uses same web app with multiple Customer databases and having shared DbContext. When user logs in based on his credentials I provide connection string to DbContext and load the data. Problem occurs when multiple user logs in at the same time and they can see each others data as DbContext keeps on switching between different users.

    I am using EF 5.0 and Autofac IOC mainly. Whats the best way to manage this?

    How can I have my DbContext maintain its data for that specific user and will not change even though other users logged in and will have different database contexts for them?

    Here's code in my Login page,

    protected void LoginBtn_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(CustomerId.Text);
                var builder = new ContainerBuilder();
                var profile = _profileProvider.GetCustomerProfile(i);
                ConnectionStringManager.ConnectionString = profile.connString;
                builder.RegisterModule<CustomerDataModule>();
                builder.Update(_container);
                Response.Redirect("Home.aspx");
         }

    This is my static variable which gives connection string

    public static class ConnectionStringManager
    {
        public static string ConnectionString { get; set; }
    }

    Here's my Module which has all entities and context classes,

    public class CustomerDataModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(c =>
            {
                ConnectionString = ConnectionStringManager.ConnectionString;
    
                return new CustomerDataContext(ConnectionString);
            })
            .As<ICustomerDataContext>()
            .As<IDbContext>()
            .InstancePerDependency();
    
            builder.RegisterType<CustomerDataContextFactory>().As<ICustomerDataContextFactory>();
    
            var assembly = Assembly.GetExecutingAssembly();
            builder.RegisterAssemblyTypes(assembly)
                   .Where(t => t.Name.EndsWith("Repository"))
                   .AsImplementedInterfaces()
                   .InstancePerDependency();
    
        }
    }

    Here's my DbContext,

    public partial class CustomerDataContext: DbContext, ICustomerDataContext
    { 
    
        public CustomerDataContext(string connectionString)
            : base(connectionString)
        { }
        .......

    Here's my One of the repository,

    public class CustomerRepository : GenericRepository<Customer, int>, ICustomerRepository
    {
        public CustomerRepository(ICustomerDataContext context)
            : base(context, customer => customer.Id)
        {
        }
    }
    Thursday, December 12, 2013 8:49 PM

Answers

  • Hello,

    >> How can I have my DbContext maintain its data for that specific user and will not change even though other users logged in and will have different database contexts for them?

    Do you mean that you want the user can watch data that belongs to them only?

    If it is, unfortunately, entity framework does not have function like this. You should add an additional column to specify this record to be whose and in the query we should add the user name clause.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 13, 2013 9:47 AM
    Moderator