简体   繁体   中英

Select entity connection string at runtime

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>  

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?

As far as I can see you have used either EF DB first or model first as a result of which the connection string was added to the app.config by Entity Framework. If the second connection string should point to the same database, add a new DataModel to your project and point it to the database of your choice and generate a model from it after which, EF makes the connection string for you.

To target the second connection string.

// by name attribute from the app.config/web.config
ConfigurationManager.ConnectionStrings["networkingEntities"].ConnectionString; connection string.
// or select by index.
var index = 0;
var adminConnectionString = ConfigurationManager.ConntectionString[++index];
var userConnectionString = ConfigurationManager.ConnectionString[index];

Edit to your answer. Hi, because I too am interested in this topic I did a quick google and found this one: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.protectsection.aspx It seems .NET has a few handy build in mechanism for us available. Tho I am also new to the topic I have no readily answer as to how it is used. GLHF

So this is how I have solved:
1. Modified the App.config like:

 <connectionstrings configsource="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=&quot;server=localhost;user id=lucian;password=lucian;persist security info=True;database=networking&quot;" 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=&quot;server=localhost;user id=root;password=lucian;persist security info=True;database=networking&quot;" providername="System.Data.EntityClient" />
  </connectionstrings>

* 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. *get the connection string like this:

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

* 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM