如何为 Elmah 使用 EntityFramework 连接字符串?

发布于 2024-07-26 08:04:50 字数 654 浏览 5 评论 0原文

在 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 连接字符串?

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="Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>

So, how can I use the EntityFramework connection string in Elmah?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑饮青盏花 2024-08-02 08:04:50

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 中)

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)

战皆罪 2024-08-02 08:04:50
    public class YourErrorLog : SqlErrorLog
    {
        public override string ConnectionString
        {
            get
            {
                //return any Connection string EF or any
            }
        }
    }

并修改配置

    <elmah>
        <errorLog type="YourAssembly.YourErrorLog, YourAssembly" connectionStringName="elmah-sqlserver" />
    </elmah>

Elmah 会询问 sql 连接字符串,但在需要时会获取您的连接字符串。

    public class YourErrorLog : SqlErrorLog
    {
        public override string ConnectionString
        {
            get
            {
                //return any Connection string EF or any
            }
        }
    }

and modify configuration

    <elmah>
        <errorLog type="YourAssembly.YourErrorLog, YourAssembly" connectionStringName="elmah-sqlserver" />
    </elmah>

Elmah will ask sql Connection string but when it need will get your connection string.

生活了然无味 2024-08-02 08:04:50

你不能——至少不能直接这样做。 您需要做的是提取 EF 连接字符串中真正引用数据库的部分(提供程序连接字符串),并将其放入 中自己的条目中web.config 的 ; 部分:

<connectionStrings>
  <add name="EducoparkELMAH"
      connectionString="Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True" 
      provider="System.SqlClient" />
</connectionStrings>

或者您可以以编程方式执行此操作 - 实体上下文将有一个名为“Connection”的属性,该属性又具有一个属性“ConnectionString”,这就是您要查找的属性:

string elmahConnectionString = EducoparkEntities.Connection.ConnectionString;

马克

You cannot - at least not directly. What you'd need to do is extract the part of the EF connection string that really references the database (the provider connection string), and put that into it's own entry in the <connectionStrings> section of your web.config:

<connectionStrings>
  <add name="EducoparkELMAH"
      connectionString="Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True" 
      provider="System.SqlClient" />
</connectionStrings>

Or you could do it programmatically - the entity context will have a property called "Connection", which in turn has a property "ConnectionString", which is the one you're looking for:

string elmahConnectionString = EducoparkEntities.Connection.ConnectionString;

Marc

仅一夜美梦 2024-08-02 08:04:50

您可以使用 Elmah.Contrib.EntityFramework nuget 包来实现此目的。

(免责声明:我写的)

You can use Elmah.Contrib.EntityFramework nuget package for this purpose.

(Disclaimer: I wrote it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文