// resolve the connection string of AppDB against the current user
        private static string ResolveUserDBConnectionStringName()
        {
            TenantRegistryEntities db = new TenantRegistryEntities();
            var userDB = db.UserProfiles.Include("Tenant").Single(u=> u.Username == HttpContext.Current.User.Identity.Name);

            return userDB.Tenant.ConnectionStringName;
        }
        // regist the Username and TenantId pair to hash table
        public static void RegisterUserTenant()
        {
            // Get tenant id from database
            TenantRegistryEntities db = new TenantRegistryEntities();
            var userDB = db.UserProfiles.Include("Tenant").Single(u=> u.Username == HttpContext.Current.User.Identity.Name);

            // check if UserTenant hash table contain the user record
            if (!UserTenantLookup.ContainsKey(HttpContext.Current.User.Identity.Name))
            {
                // add to hash table if user was not found in hash table (new login)
                UserTenantLookup.Add(HttpContext.Current.User.Identity.Name, userDB.Tenant.id);
            }
        }
        public ActionResult CreateLogin(FormCollection form, TenantLoginViewModel model)
        {
            //todo: implement role for users!  Please design the architecture first!!!
            var tenantDb = new TenantRegistryEntities();
            var tenant = new Tenant
            {
                id = System.Guid.NewGuid().ToString(),
                Name = model.Tenant.Name,
                db = System.Web.Configuration.WebConfigurationManager.AppSettings["NewTenantDatabaseName"],  // todo: Default Tenant db must be get from a separate class
                ConnectionStringName = System.Web.Configuration.WebConfigurationManager.AppSettings["NewTenantConnectionString"] // todo: Tenant connection string must be get from a separate class
            };

            var userprofile = new UserProfile
            {
                Username = model.UserProfile.Username,
                FirstName = model.UserProfile.FirstName,
                LastName = model.UserProfile.LastName
            };

            // Save tenant
            tenant.UserProfiles.Add(userprofile);
            tenantDb.Tenants.AddObject(tenant);
            tenantDb.SaveChanges();

            // Create asp.net login
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var pw = form["Password"].ToString();
                var email = form["Email"].ToString();
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserProfile.Username, pw, email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    //FormsService.SignIn(model.UserProfile.Username, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            return View("Index");
        }
        // GET: /Test/ListCustomer
        public ActionResult ListCustomer()
        {
            var tenantDb = new TenantRegistryEntities();
            var appDb = new AppDBEntities(UserProfileHelper.ResolveUserDBConnectionString());

            // Register Username and TenantId to memory
            UserProfileHelper.RegisterUserTenant();

            // get tenant id from memory variable
            var tid = UserProfileHelper.UserTenantLookup[User.Identity.Name].ToString();
            var customers = appDb.Customers.Where(c => c.TenantId == tid);

            return View(customers);
        }
        // GET: /Test/ListAllCustomer
        public ActionResult ListAllCustomer()
        {
            var tenantDb = new TenantRegistryEntities();
            var appDb = new AppDBEntities(UserProfileHelper.ResolveUserDBConnectionString());

            var customers = appDb.Customers.ToList();

            return View(customers);
        }