Dynamically changing database connections in Entity Framework 5

Source: Internet
Author: User

Upgrade Silverlight to 5,ado today. ENT EF also upgraded to 5 with NuGet. It turns out that the EF at 5 does not have 4 of that sort of partial method by default.

Of course, you can replace the properties of the generator with the default, which is note. You can also have partial methods as in version 4.

But I found the EF5 below more intelligent. Better use. The link string for EF has a related class

EntityConnection

It can be manipulated.

See MSDN:

Http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx

This makes it more flexible to change the database dynamically.

Just customize a method and then return the string to the entityconnection link to

Specify the provider name, server and database. string providerName = "System.Data.SqlClient"; string serverName = "."; String databaseName = "AdventureWorks";//Initialize the connection string builder for the //underlying provider. SqlConnectionStringBuilder Sqlbuilder = new SqlConnectionStringBuilder ();//Set the properties for the data source.sqlb Uilder. DataSource = Servername;sqlbuilder.initialcatalog = Databasename;sqlbuilder.integratedsecurity = true;//Build the SqlConnection connection String. string providerstring = sqlbuilder.tostring ();//Initialize the Entityconnectionstringbuilder.entityconnectionstringbuilder Entitybuilder = new Entityconnectionstringbuilder ();// Set the provider Name.entityBuilder.Provider = providername;//Set the provider-specific connection String.entitybuilder . providerconnectionstring = providerstring;//Set the Metadata location.entityBuilder.Metadata = @ "res://*/                            adventureworksmodel.csdl| Res://*/adventureworksmodel.ssdl| RES://*/ADVENTUREWORKSMODEL.MSL "; Console.WriteLine (Entitybuilder.tostring ());

Dynamically changing database connections in Entity Framework 5

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.