asp.net – 如何使用Elmah的EntityFramework连接字符串?

在ELMAH中将错误记录到数据库中,您可以编写:

<errorLog type="Elmah.SqlErrorLog, Elmah"
            connectionStringName="EducoparkEntities"/>

但是,如果我使用EntityFramework,这不起作用,因为EF的连接字符串也包含元数据:

<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"/>

那么,我如何在Elmah中使用EntityFramework连接字符串?

1

您可以通过实体框架中提供的ConnectionStringBuilder提取数据库连接字符串.

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

要将该数据库连接字符串插入Elmah,您必须在Application_Start上设置它(在Global.asax中)

翻译自:https://stackoverflow.com/questions/1083558/how-to-use-entityframework-connection-string-for-elmah

转载注明原文:asp.net – 如何使用Elmah的EntityFramework连接字符串?