asp.net mvc的多个连接字符串 [英] asp.net mvc multiple connection strings

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

问题描述

我创建使用MVC 3.0 asp.net MVC应用程序。我有2个用户,但该数据库是一样的。那么,是否可以设置两个连接字符串,甚至在web.config中吗?当用户登录,我把它重定向到他的数据库,所以后来他可以用他的数据库。

I am creating asp.net MVC Application using MVC 3.0. I have 2 users but the DataBase is the same. So, Is it possible to setup two connection strings or even more in web.config? when user login, I redirect it to his database, so then he can use his DataBase.

所以,主要的问题是在这里找出哪些用户登录并使用的连接字符串该用户。

So major issue here is to find out which user is logged in and use connection string for that user.

我使用MVC默认帐户控制器和例如,当我要为用户在我看来,显示欢迎信息I型:如果(@ User.Identity.Name ==用户名),然后一些消息
那么,是找出哪些用户登录,并在控制器或视图中设置了连接字符串的最佳地点?

I am using default mvc account controller and for example when i want to display welcome message for user in my view i type: if (@User.Identity.Name == "UserName") then some message So where is the best place to find out which user is logged in and set his connection string in controller or in a view?

推荐答案

我同意的Jakub的回答是:<一href=\"http://stackoverflow.com/questions/6491982/asp-net-mvc-multiple-connection-strings/6492310#6492310\">there是处理多租户不是每个用户不同的数据库更好的办法。

I agree with Jakub's answer: there are better ways of handling multi-tenancy than having a different database per user.

不过,要回答你的具体问题,也有浮现在脑海两个选项:

However, to answer your specific question, there are two options that come to mind:


  1. 您可以登录后立即设置连接字符串一个会话变量。

  2. 您的数据访问层可以选择基于它的创建时登录的用户的连接字符串。 (我建议这在第一个选项)

  1. You can set the connection string to a session variable immediately after login.
  2. Your data access layer can choose the connection string based on the logged in user when it's created. (I'd recommend this over the first option)


  1. 要保存登录后的连接,如果你使用标准的ASP.NET MVC的帐户控制器,看看登录后动作:

  1. To store the connection after login, if you're using the standard ASP.NET MVC Account Controller, look at the LogOn post action:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            //EXAMPLE OF WHERE YOU COULD STORE THE CONNECTION STRING
            Session["userConnectionString"] = SomeClass.GetConnectionStringForUser(model.UserName);

            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}


  • 如果你想在做的数据访问时选择连接字符串,资源库或数据访问层将可能有用于处理的系统。例如实体框架code首先,该构造的DbContext 允许您在连接字符串的名称传递,当你创建它:

  • If you wanted to select the connection string when doing data access, your repository or data access layer will probably have a system for handling that. For instance with Entity Framework Code First, the DbContext constructor allows you to pass in the name of a connection string when you're creating it:

    的connectionString = SomeClass.GetConnectionStringForUser(model.UserName);
    的DbContext语境=新的DbContext(的connectionString);

    connectionString = SomeClass.GetConnectionStringForUser(model.UserName); DbContext context = new DbContext(connectionString);

    但同样,我会看的,除非你的业务决定了你的用户有物理上独立的数据库处理多重任务处理的其他方式。

    But again, I'd look at other ways of handling multitenancy unless your business dictates that your users have physically separate databases.

    这篇关于asp.net mvc的多个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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