C # Constructs a Entityframe database connection string based on the ADO database connection string

Source: Internet
Author: User
Tags connectionstrings

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=&quot;data Source=XXXXX; Initial catalog=xxxxx;persist security Info=true;user id=xxxx;password=xxxxx; Multipleactiveresultsets=true; app=entityframework&quot; "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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.