Reusing an existing database connection string in the Entity Framework

Source: Internet
Author: User

This article reproduced: http://www.cnblogs.com/dudu/archive/2011/01/29/entity_framework_connection_string.html

The connection string used by the Entity framework is different from ADO, see:

The connection string for the ado.net,entity framework is not only to hold the metadata configuration information, but also to hold the full database connection string (in the "Provider connection string" section).

There are two disadvantages to this design:

1. Connection string configuration is complex;

2. The existing ADO. DB connection string cannot be reused.

I wrote this in the technical promotion: Public classdalbase{ PublicNwentities Nwcontext {Get;Set; }  Publicdalbase () {stringProviderString =Configurationmanager.connectionstrings[connectionstringname].        ConnectionString; stringconn =getentityconnstring (providerstring); Nwcontext=Newnwentities (conn); }     Private stringGetentityconnstring (stringproviderstring) {Entityconnectionstringbuilder Entitybuilder=NewEntityconnectionstringbuilder (); //The value of the metadata property is pasted from the config generated by the wizard .Entitybuilder.metadata ="RES://*/NW.CSDL|RES://*/NW.SSDL|RES://*/NW.MSL"; Entitybuilder.providerconnectionstring=providerstring; Entitybuilder.provider="System.Data.SqlClient"; returnentitybuilder.tostring (); }}

Note the above Entitybuilder.metadata = "RES://*/NW.CSDL|RES://*/NW.SSDL|RES://*/NW.MSL";

Metadata: Indicates the path of the. CSDL/.SSDL/.MSL three files to match the name of your edmx file.

The Web. config file is as follows:

Entity freamwork configuration file mode
<add name="lgscmsentities"connectionstring="metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=system.data.sqlclient; Provider Connection string=&quot; server=zengfanlong;database=lgscms;uid=sa;pwd=123456;&quot;"Providername="System.Data.EntityClient"/>

Ado. NET mode configuration file <add name="lgscmsentities"connectionstring="server=zengfanlong;database=lgscms;uid=sa;pwd=123456;"/>

Reusing an existing database connection string in the Entity Framework

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.