How to use EntityFramework connection string for Elmah?

.net, asp.net, elmah, entity-framework

Solution

1

You can extract the database connection string via the ConnectionStringBuilder provided in the entity framework.

private string ExtractConnectionStringFromEntityConnectionString(string entityConnectionString)
{
    // create a entity connection string from the input
    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(entityConnectionString);

    // read the db connectionstring
    return entityBuilder.ProviderConnectionString;
}

2

To plug that db connection string into Elmah you will have to set it on Application_Start ( in Global.asax)

Problem

In ELMAH for logging errors to the database you can write: ``` <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="EducoparkEntities"/> ``` However, if I use EntityFramework, this doesn't work because the connection string for EF contains metadata as well: ``` <add name="EducoparkEntities" connectionString="metadata=res://*/EducoparkData.csdl|res://*/EducoparkData.ssdl|res://*/EducoparkData.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/> ``` So, how can I use the EntityFramework connection string in Elmah?

Original source

Related problems