如何对实体框架不同用户的不同连接字符串 [英] How to have different connection string for different users in Entity Framework

查看:169
本文介绍了如何对实体框架不同用户的不同连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我需要为不同的用户不同的连接字符串的要求。这个想法是让用户名密码在登录屏幕上被用作用户名和密码连接字符串。从而使应用程序能够使用不同的连接字符串为不同的用户,并在整个应用程序中使用此连接字符串。

I have a requirement in which I need to have different connection strings for different users. The idea is to have the username and password supplied at the login screen to be used as the username and password of the connection string. Thus making application to use different connection string for different user, and to use this connection string throughout the application.

如何得到这个设置在EF 4.1

How to get this setup in EF 4.1

PS:我用的DbContext

推荐答案

感谢凯文·容汉斯

这是我如何都做到了。

在模型上下文类

public class MyEntities : DbContext
{
    public MyEntities (string connectionString)
        : base(connectionString)
    {
    }

然后在登录控制器

then in the login controller

var dataConnection = WebConfigurationManager.OpenWebConfiguration("/").ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString;
dataConnection = dataConnection.Substring(0, dataConnection.LastIndexOf("\"")) + ";USER ID=" + userName +";Password=" + password + "\"";
Session["connectionString"] = dataConnection;

和来自其他地方。

and the from else where

var _db = new MyEntities (Session["connectionString"].ToString());

这篇关于如何对实体框架不同用户的不同连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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