每个租户的asp.net mvc多租户数据库 [英] asp.net mvc multitenant database per tenant

查看:68
本文介绍了每个租户的asp.net mvc多租户数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的公司构建一个应用程序,该应用程序需要每个客户端有单独的数据库.该应用程序供其他多个公司使用,因此该应用程序需要在用户登录时标识公司名称,并使用户仅在其公司数据库内操作.我已经设置好了,但是问题是该应用程序无法同时处理2个不同的数据库.当来自两个不同公司的用户登录时,第一个用户的数据库将更改为已登录的第二个用户的数据库!这当然是不可接受的.如何使应用程序同时使用2个数据库?

I'm building an app for our company which needs to have separate database per client. App is for the usage of other multiple companies, so the app needs to identify the company name when the user logs in and make the users operate only within their company db. I have it all set, but the problem is that the app is not able to handle 2 different databases simultaneously. When users from two different companies log in, the first users db gets changed to the db of the second user who is logged in! This is of course unacceptable. How can I make the app to use 2 dbs simultaneously?

我有一个数据库,该数据库收集所有应用程序用户及其公司名称,以及每个公司的单独数据库.我也有一个标准的ASP以下是我的代码:

I have one database which collects all app users and their company names and separate databases for each company. I also have a standard asp below are my codes:

帐户控制器中的登录类和数据库初始化程序

Login class and database initializer in account controller

        [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        bool ifDemo = false;
        string demoPrefix = "demo.";
        if (model.Email.StartsWith(demoPrefix))
        {
            ifDemo = true;
            model.Email = model.Email.Substring(demoPrefix.Length);
        }

        SetDatabaseInitializerAndInitializeIt(ifDemo, model.Email);


        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

        switch (result)
        {
            case SignInStatus.Success:
                returnUrl = CheckFirstLogin(model.Email, returnUrl);
                        await OnSignInSuccess(model);
                //FormsAuthentication.SetAuthCookie(model.Email, false);
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

    public static void SetDatabaseInitializerAndInitializeIt(bool demoDB, string login)
    {
        Database.SetInitializer(new ApplicationUsersSeedData());
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<Facility.Data.FacilityEntities,
            Facility.Migrations.Configuration>());

        // Check current users company name and set the right database. 
        // To use demo version of database add "demo." prefix to the login email e.g: demo.testspamu79@gmail.com

        using (var domain = new Facility.Models.UserCommonDBContext())
        {
            domain.Database.Initialize(true);
        }

        UserCommonDBContext context = new Facility.Models.UserCommonDBContext();

        var companyName = context.CommonUser.Where(x => x.CommonUserEmail == login).FirstOrDefault().CommonUserCompanyName;
        if (demoDB)
            companyName = companyName + "Demo";

        using (var domain = new Facility.Data.FacilityEntities(companyName))
        {
            domain.Database.Initialize(true);
        }

    }

Dbcontext:

Dbcontext:

    public partial class FacilityEntities : DbContext
{
    public static string DbName;

    public FacilityEntities() : base(string.IsNullOrWhiteSpace(DbName) ? "Demo" : DbName)
    {
    }
    public FacilityEntities(string dbName) : base(dbName)
    {
        DbName = dbName;
    }

正如Tielson T.在评论中所说,

推荐答案

,我在会话中删除了静态的和存储的数据库名称,现在可以使用了!

as Tielson T. said in the comments, I got rid of static and stored db name in session, and now it works!

这篇关于每个租户的asp.net mvc多租户数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆