Welcome to weblogs.com.pk Sign in | Join | Help

Real-world Entity Framework :: Connection Strings

Entity Framework is nice addition to .NET developer toolbox; but like most of the things there are some tricks in using it in real-world. The first hurdle that I guess most of us face; having multiple models; but pointing to same database. The Visual Studio EDM Designer (The Model First Workflow) tends to like having connection strings for every model and to keep it happy; its OKAY to have multiple connection strings. Generally its not a big deal; as usually the EDM and data access classes are in its own separate assembly / class library and one can have single connection string and reuse it in each model accordingly encapsulating the EDM / Visual Studio constraint. Here is one such Connection String Helper; that I find keep referencing to in almost all projects!

using System.Data.EntityClient;
using System.Data.SqlClient;
 
namespace ConsoleApp
{
    static class ConnectionStringHelper
    {
        public static string GetSqlConnectionString(string serverName, string databaseName, string userName, string password)
        {
            var sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            if (string.IsNullOrEmpty(userName))
                sqlBuilder.IntegratedSecurity = true;
            else
            {
                sqlBuilder.UserID = userName;
                sqlBuilder.Password = password;
            }
            return sqlBuilder.ToString();
        }
 
        public static string GetEntityFrameworkConnectionString(string entityModelName, string prefixOfModel, string providerConnectionString)
        {
            string providerName = "System.Data.SqlClient";
            var entityBuilder = new EntityConnectionStringBuilder();
 
            entityBuilder.Provider = providerName;
            entityBuilder.ProviderConnectionString = providerConnectionString;
 
            if (!string.IsNullOrEmpty(prefixOfModel))
                entityBuilder.Metadata = string.Format(@"res://*/{0}.{1}.csdl|res://*/{0}.{1}.ssdl|res://*/{0}.{1}.msl", prefixOfModel, entityModelName);
            else
                entityBuilder.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", entityModelName);
            return entityBuilder.ToString();
        }
 
        public static string GetEntityFrameworkConnectionString(string entityModelName, string providerConnectionString)
        {
            return ConnectionStringHelper.GetEntityFrameworkConnectionString(entityModelName, string.Empty, providerConnectionString);
        }
 
        public static string GetEntityFrameworkConnectionString(string entityModelName, string serverName, string databaseName, string userName, string password)
        {
            string providerString = ConnectionStringHelper.GetSqlConnectionString(serverName, databaseName, userName, password);
            return ConnectionStringHelper.GetEntityFrameworkConnectionString(entityModelName, providerString);
        }
    }
}

With such approach in place; its needed that generated Context class’s default constructor; which tries to find the connection string under the name specified at design time to be private. For smaller projects; one can check it by using Visual Studio’s Find References by opening up the Model.Designer.cs class; but in enterprise scale app; you might want to have better control. We can add ADO.NET * Generators; which will give you a T4 template which then generates the entity / context classes as per the template; one can easily edit the template as per requirements!

ADONET Generators

Published Tuesday, January 28, 2014 3:40 PM by khurram

Comments

# re: Real-world Entity Framework :: Connection Strings

Wednesday, January 29, 2014 11:44 AM by khurram

http://msdn.microsoft.com/en-us/data/jj613116.aspx has more information about different available Generators

New Comments to this post are disabled