Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a SQL connection string with ADO.NET Entity Data Model

I am trying to use the ADO.NET Entity Data Model in a way that I can on the fly change which database I point too. Changing databases may require an entirely new connection string. Some databases are on different servers. So I need the ability to pass my ADO.NET Entity Data Model a custom connection string formated like so 'server=severaddress;database=database1;User ID=test;Password=test1234;'

Edited: My Entity Class implements ObjectContext. The three constructers I can use is the default, pass in connectionString, pass in an EntityConnection. When ever I use the overload constructers I get errors saying it doesn't recognize "server" in the connectionstring.

I need to either instantiate my repository with a custom connection string, or be able to set it before use.

like image 499
Jason Zambouras Avatar asked Mar 03 '11 18:03

Jason Zambouras


People also ask

What is the syntax of the connection string in Ado net?

Learn about syntax of connection strings in ADO.NET. The syntax for each provider is documented in its ConnectionString property. Gets or sets the string used to open a SQL Server database. This article describes how to programmatically specify the client network library in the connection string when you connect to a SQL Server database.

What is a connection string in Entity Framework?

Connection Strings in the ADO.NET Entity Framework. A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.

What is ADO net sqlconnection class in C#?

What is ADO.NET SqlConnection Class in C#? The ADO.NET SqlConnection class belongs to System.Data.SqlClient namespace and is used to establish an open connection to the SQL Server database. The most important point that you need to remember is the connection does not close implicitly even it goes out of scope.

How do I access entityconnection in ObjectContext?

The EntityConnection used by an ObjectContext instance can be accessed from the Connection property. For more information, see Managing Connections and Transactions. To learn about the general syntax for connection strings, see Connection string syntax | Connection Strings in ADO.NET.


1 Answers

ObjectContext accepts entity connection string in its constructor. Entity connection string consists of three parts:

  • Metadata location (location of mapping XMLs produced by EDMX file)
  • Data storage provider
  • Data store connection string (that is what you want to provide)

You have several ways to achieve what you want. Generally what you need is combine two parts of connection string:

string format = "metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"";
string connectionString = "server=severaddress;database=database1;UserID=test;Password=test1234;"

var context = ModelContext(String.Format(format, connectionString));

The format describes location of metadata from Model.edmx included as resources in assembly and Sql provider. Second part is your connection string.

Be aware that this will only work if all your databases have same schema and use same provider.

like image 105
Ladislav Mrnka Avatar answered Nov 09 '22 17:11

Ladislav Mrnka