2连接字符串 - 实体框架 - 在运行时选择 [英] 2 Connection Strings - Entity Framework- select at runtime

查看:72
本文介绍了2连接字符串 - 实体框架 - 在运行时选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到MYSQL数据库的应用程序。我使用Entity Framework完成所有工作。

现在当我第一次安装时,我设置了实体,并产生了这样的连接字符串:



I have an application that connects to a MYSQL database. I use Entity Framework to do all the job.
Now When I first Installed, I set up entity, and resulted in a connection string like this:

<connectionStrings>
    <add name="networkingEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=lucian;persist security info=True;database=networking"" providerName="System.Data.EntityClient"/>
  </connectionStrings>







现在,我的应用程序有2个用户:管理员和学生。



Form1 =>更新数据库和服务器的信息



登录表格=>用户认证



MainForm =>所有行动都参与其中。如果管理员已登录,他可以修改数据库中的内容



数据库:

有2个用户:root和lucian。



lucian是限制用户...





现在,我的问题是:



如何在现有的连接字符串中添加第二个连接字符串,并在运行时选择一个?我的意思是,当Form1运行时,将线程置于休眠状态,选择第二个连接字符串然后,转到登录表单,以管理员身份登录并使cnahge犯罪数据库?







如何从外部文件中获取连接字符串的登录信息?




Now, my application has 2 users: an admin and a student.

Form1 => updates information from database and from server

Login form=> the users authentificate

MainForm => where all the action takes part. If the admin is logged in he can modify contents in the database

The database:
has 2 users: root and lucian.

"lucian" is a limited user...


Now, my problem is:

How can I add a second connection string to the already existing one, and select that one at runtime? I mean, when Form1 is running, put the thread to sleep, select the second connections string and then, go to Login form, login as admin and make the cnahge sin the database?



And how can I get tho login information from the connection string, from an external file?

推荐答案

要动态更改实体框架的连接,您必须将连接字符串传递给DataContext类的构造函数,如下所示。

To change connection of Entity Framework dynamically, you will have to pass connection string to the constructor of DataContext class as below.
string connection = "YourConnectionString";
DataContext myDataContext = new DataContext(connection);




Quote:

我怎样才能从外部文件中获取连接字符串的登录信息?

And how can I get tho login information from the connection string, from an external file?



您可以使用web.config / app.config之类的配置文件来维护您的连接字符串并访问它,如下面的链接所示。



http:// social。 msdn.microsoft.com/Forums/en-US/linqtosql/thread/45527f5e-a380-490f-84d5-88c31cb9a03a/


这就是我解决的问题:

1。修改了App.config,如:

So this is how I have solved:
1. Modified the App.config like:
<connectionstrings configsource="DatabaseConnectionDetails.config" />





这是DatabaseConnectionDetails.config:





and this is DatabaseConnectionDetails.config:

<connectionstrings>
  <add name="networkingEntities" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=lucian;password=lucian;persist security info=True;database=networking"" providername="System.Data.EntityClient" />
  <add name="networkingEntitiesAdmin" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=lucian;persist security info=True;database=networking"" providername="System.Data.EntityClient" />
</connectionstrings>





2.为Model1.Context.tt模板添加了一个带字符串参数的构造函数:





2.Added a contructor with string parameter to Model1.Context.tt template:

  public <#=code.Escape(container)#>(string myString)
        : base(myString)
    {
<#>
if (!loader.IsLazyLoadingEnabled(container))
{
#>
        this.Configuration.LazyLoadingEnabled = false;
<#>
}
#>
    }





3.获取如下连接字符串:





3. get the connection string like this:

string  str = ConfigurationManager.ConnectionStrings["networkingEntitiesAdmin"].ConnectionString;





4.每当我想使用上下文时,我都会使用(例如):





4. And whenever I want to use the context , I use(for example):

 networkingEntities net=new networkingEntities(str);


public List<utilizator> ListaUtilizatori()
        {
            
            var query = from u in net.utilizator
                        select u;
            List<utilizator> users = new List<utilizator>();
            try
            {
                users = query.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return users;
        }







现在,我仍然需要了解如何加密配置文件。 ...

谢谢大家的帮助......




Now, I still have to discover how I encrypt the config files....
Thank you all for help...


这篇关于2连接字符串 - 实体框架 - 在运行时选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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