Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<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>



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?
Posted
Updated 14-May-13 8:23am
v2
Comments
Roman Zinnatov 14-May-13 15:23pm    
Please, let me ask you some questions.
First of all. Two connection strings will be differ by login credentials. One for each user? Am I right?
Luci C 14-May-13 15:25pm    
that's right...
Luci C 14-May-13 15:24pm    
yes, that's right...
Roman Zinnatov 14-May-13 15:35pm    
What if required to add another user? May be you should implement Membership in you app. It provides user's groups. So you will be able differentiate user's rights at application level.
Luci C 14-May-13 15:38pm    
for all users I will use the first login details
for admin I will use the second login details.

Depending on which login details I use, I can do operations on the database (I have 2 separate users in the database, one limited and root)

To change connection of Entity Framework dynamically, you will have to pass connection string to the constructor of DataContext class as below.
C#
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?

You may use configuration file like web.config/app.config to maintain your connection string and access it as shown in below link.

http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/45527f5e-a380-490f-84d5-88c31cb9a03a/
 
Share this answer
 
Comments
Luci C 14-May-13 15:18pm    
but I don't use a dataContext class... It's all done by entity framework...
RaisKazi 14-May-13 15:23pm    
You should first learn how Entity Framework creates connecion. Have a look at below link.
Click Here
So this is how I have solved:
1. Modified the App.config like:
C#
<connectionstrings configsource="DatabaseConnectionDetails.config" />


and this is DatabaseConnectionDetails.config:

C#
<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.Added a contructor with string parameter to Model1.Context.tt template:

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


3. get the connection string like this:

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


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

C#
 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...
 
Share this answer
 
v2
Comments
RaisKazi 14-May-13 16:56pm    
Isn't the same way I suggested earlier? networkingEntities is your DataContext Class.
Luci C 14-May-13 17:01pm    
yes.. but it took me a while to figure how to do it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900