2 Using the Data Providers : Using Connection Strings

Using Connection Strings
You can define the behavior of a connection using a connection string or the properties of the data provider’s Connection object. However, values set in the connection string cannot be changed by the connection properties.
The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The following example shows the keywords and values for a simple connection string for the Oracle data provider:
"Host=Accounting1;Port=1521;Service Name=ORCL;User ID=scott;Password=tiger"
Use the following guidelines when specifying a connection string:
You can also use single quotes when the value starts with a double quote. Conversely, double quotes can be used if the value starts with a single quote. If the value contains both single quotes and double quotes, the character used to enclose the value must be doubled every time it occurs within the value.
The Equals character (=) can also be repeated within the connection string. For example:
Initialization String="update mytable set col1 == 'foo'"
"Host=Accounting1;Port=1521;Service Name=ORCL; Connection Timeout=15;User ID=scott;Password=tiger;Connection Timeout=35"
Appendix B “Connection String Option Descriptions” contains detailed information about supported connection string options:
See “DB2 Data Provider Connection String Options” for information for the DB2 data provider and DB2 Entity Framework data provider.
See “Oracle Data Provider Connection String Options” for information for the Oracle data provider and Oracle Entity Framework data provider.
See “SQL Server Data Provider Connection String Options” for information for the Microsoft SQL Server data provider.
See “Sybase Data Provider Connection String Options” for information for the Sybase data provider and Sybase Entity Framework data provider.
Connecting to Entity Framework Applications
NOTE: This section applies to the Oracle Entity Framework data provider Version 4.0.
When a Code First application does not provide a connection or name when creating a DbContext, the Entity Framework uses the DefaultConnectionFactory to create a DbConnection object to service the context. By default, the Entity Framework uses the SqlCeConnectionFactory class but you can override this using the data provider’s ConnectionFactory interface.
Connect for ADO.NET Entity Framework data providers include an implementation of the IDbConnectionFactory interface that developers can use to make the provider the default. See the specific data provider chapters for an example
Below is a simple Code First application that inserts a row into the database.
class SimpleCodeFirst {
public static void Main(string[] args) {
 
RkoContext context = new RkoContext(args[0]);
RkoPerson person = new RkoPerson();
person.Name = "John Smith";
context.RkoPeople.Add(person);
context.SaveChanges(); // performs insert
}
}
 
public class RkoPerson {
 
public int Id { get; set; }
public string Name { get; set; }
}
 
public class RkoContext : DbContext {
 
public RkoContext(string connectionName) : base(connectionName) { }
public DbSet<RkoPerson> RkoPeople { get; set; }
}