c# - Select entity connection string at runtime -
i have application connects mysql database. use entity framework job. when first installed, set entity, , resulted in connection string 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, application has 2 users: admin , student.
form1 => updates information database , server
login form=> users authentificate
mainform => action takes part. if admin logged in can modify contents in database
the database: has 2 users: root , lucian.
"lucian" limited user...
now, problem is:
how can add second connection string existing one, , select 1 @ runtime? mean, when form1 running, put thread sleep, select second connections string , then, go login form, login admin , make cnahge sin database?
and how can tho login information connection string, external file?
so how have solved:
1. modified app.config like:
<connectionstrings configsource="databaseconnectiondetails.config" />;
and 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.*added contructor string parameter model1.context.tt template:
public <#=code.escape(container)#>(string mystring) : base(mystring) { <# if (!loader.islazyloadingenabled(container)) { #> this.configuration.lazyloadingenabled = false; <# } #> }
*3.*get connection string this:
string str = configurationmanager.connectionstrings["networkingentitiesadmin"].connectionstring;
*4.*and whenever want use context , use(for example):
networkingentities net=new networkingentities(str); public list<utilizator> listautilizatori() { var query = 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, still have discover how encrypt config files.... thank help...
Comments
Post a Comment