In order to maintain development efficiency and keep the code elegant, Entityframe is referenced in the project. However, because some report functions require a lot of computation, it is also required to directly use ADO, call the stored procedure to calculate.
There are two types of database connection strings in the Webconfig file.
<!--Entityframe -<connectionStrings><Addname= "Gpsdbentities"connectionString= "metadata=res://*/models.dbmodels.xxxx.csdl|res://*/models.dbmodels.xxxx.ssdl|res://*/ Models.dbmodels.xxxx.msl;provider=system.data.sqlclient;provider connection String="data Source=XXXXX; Initial catalog=xxxxx;persist security Info=true;user id=xxxx;password=xxxxx; Multipleactiveresultsets=true; app=entityframework" "ProviderName= "System.Data.EntityClient" /></connectionStrings><!--ADO -<AddKey= "sqlConnectionString"value= "Server=xxxxx;uid=xxxxx;pwd=xxxx;database=xxxx"></Add>
This can be a cumbersome way to write, why the same database, I have to write two times the connection string.
So I want to keep only the ADO database connection string, when using Entityframe, production Entityframe dedicated database connection string to connect.
So I built a tool class to generate the Entityframe database connection string.
Public Sealed classdbconnectionutil{Private Static stringIP {Get;Set; } Private Static stringUserId {Get;Set; } Private Static stringPassword {Get;Set; } Private Static stringDBName {Get;Set; } Public Static string Entityconnectionstr{Get;Set; } Public Static ReadOnlyDbconnectionutil instance =NewDbconnectionutil (); PrivateDbconnectionutil () {getdbsetting (); } Private voidgetdbsetting () {varConnectstr = configurationmanager.appsettings["sqlConnectionString"]; varSettingarray = Connectstr.split (';'); foreach(varSettinginchSettingarray) { varKeyval = setting. Split ('='); Switch(keyval[0]) { Case "Server": IP = keyval[1]; Break; Case "UID": UserId = keyval[1]; Break; Case "pwd": Password = keyval[1]; Break; Case "Database": DBName = keyval[1]; Break; }} entityconnectionstr ="metadata=res://*/models.dbmodels.gpsdb.csdl|res://*/models.dbmodels.gpsdb.ssdl|res://*/ Models.dbmodels.gpsdb.msl;provider=system.data.sqlclient;provider connection string=\ "Data source="+ IP +"; initial catalog="+ DBName +";p ersist Security Info=true;user id="+ UserId +";p assword="+ Password +"; Multipleactiveresultsets=true; App=entityframework\ ""; }}
It can then be called directly when it is used.
Public Partial class xxxxx:dbcontext{ public XXXXX () base( Dbconnectionutil.entityconnectionstr) { } protectedoverride void onmodelcreating (Dbmodelbuilder modelBuilder) { thrownew unintentionalcodefirstexception (); }}
C # Constructs a Entityframe database connection string based on the ADO database connection string