Reuse an existing database connection string in Entity Framework

Source: Internet
Author: User
Tags connectionstrings
Http://www.cnblogs.com/dudu/archive/2011/01/29/entity_framework_connection_string.html

The connection string used by Entity Framework is different from that used by ADO. net. For details, see:

Compared with ADO. net, the connection string of Entity Framework not only stores metadata configuration information, but also stores the complete database connection string (in the "provider connection string" section ).

This design has two shortcomings:

1. The connection string configuration is complex;

2. Existing ADO. NET database connection strings cannot be reused.

I think a more reasonable design should be to separate the database connection string and provide a "provider connection string name" setting. In this setting, you can specify the name of "database connection string, see:

This is simple and reusable!

After YY, I still want to return to reality. Entity Framework is just like this. I just want to reuse the existing database connection string. What should I do?

Create your own entityconnection and pass it to the objectcontext constructor,CodeAs follows:

 Public  Cnblogsobjectcontext ()
: Base (Buildconnection ( " Cnblogsdb " ), " Club_cnblogscontainer " )
{
}

Static Entityconnection buildconnection ( String Connectionstringname)
{
String Connectionstring = Configurationmanager. connectionstrings [connectionstringname]. connectionstring;
Metadataworkspace = New Metadataworkspace ( New String [] { " Res ://*/ " },
New Assembly [] {assembly. getexecutingassembly ()});

Sqlconnection = New Sqlconnection (connectionstring );
Return New Entityconnection (workspace, sqlconnection );
}

The connection string used by Entity Framework is different from that used by ADO. net. For details, see:

Compared with ADO. net, the connection string of Entity Framework not only stores metadata configuration information, but also stores the complete database connection string (in the "provider connection string" section ).

This design has two shortcomings:

1. The connection string configuration is complex;

2. Existing ADO. NET database connection strings cannot be reused.

I think a more reasonable design should be to separate the database connection string and provide a "provider connection string name" setting. In this setting, you can specify the name of "database connection string, see:

This is simple and reusable!

After YY, I still want to return to reality. Entity Framework is just like this. I just want to reuse the existing database connection string. What should I do?

Create your own entityconnection and pass it to the objectcontext constructor. The Code is as follows:

 Public  Cnblogsobjectcontext ()
: Base (Buildconnection ( " Cnblogsdb " ), " Club_cnblogscontainer " )
{
}

Static Entityconnection buildconnection ( String Connectionstringname)
{
String Connectionstring = Configurationmanager. connectionstrings [connectionstringname]. connectionstring;
Metadataworkspace = New Metadataworkspace ( New String [] { " Res ://*/ " },
New Assembly [] {assembly. getexecutingassembly ()});

Sqlconnection = New Sqlconnection (connectionstring );
Return New Entityconnection (workspace, sqlconnection );
}

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.