Recently I began to play with ADO.NET Entity Framework Beta 3. I didn't liked the way of setting connections. My requirements were not to add new lines to existing config file, just to use existing defined connectionString.
The solution I found is to use EntityConnectionStringBuilder class.
Here is the method used to create the connection:
public static EntityConnection Create(string connectionName, Type objectContextType) { if (string.IsNullOrEmpty(connectionName)) throw new ArgumentNullException("connectionName"); ConnectionStringSettings csSettings = null; if ((csSettings = ConfigurationManager.ConnectionStrings[connectionName]) == null) throw new ArgumentException("connectionName could not be found"); EntityConnectionStringBuilder connBuilder = new EntityConnectionStringBuilder(); connBuilder.ProviderConnectionString = csSettings.ConnectionString; connBuilder.Provider = csSettings.ProviderName; connBuilder.Metadata = string.Format("res://{0}/", objectContextType == null ? "*" : Assembly.GetAssembly(objectContextType).FullName); return new EntityConnection(connBuilder.ToString()); }
The method takes 2 parameters: connectionName and objectContextType.
- connectionName: the name of the connection from App.config or Web.config
- objectContextType: is the type of entity container. It is used to point metadata to the assembly that contains metadata artifacts (CSDL, MSL, SSDL). If it is null it will tell Entity Framework to search all referenced assemblies to locate metadata artifacts.
This will work only if metadata artifacts is set to Embeded in Output Assembly.
No comments:
Post a Comment