如何为 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="Data Source=(Local);Initial Catalog=...;User Id=...;Password=...;MultipleActiveResultSets=True"" 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1
可以通过实体框架中提供的ConnectionStringBuilder提取数据库连接字符串。
2
要将数据库连接字符串插入 Elmah,您必须在 Application_Start 上设置它(在 Global.asax 中)
1
You can extract the database connection string via the ConnectionStringBuilder provided in the entity framework.
2
To plug that db connection string into Elmah you will have to set it on Application_Start ( in Global.asax)
并修改配置
Elmah 会询问 sql 连接字符串,但在需要时会获取您的连接字符串。
and modify configuration
Elmah will ask sql Connection string but when it need will get your connection string.
你不能——至少不能直接这样做。 您需要做的是提取 EF 连接字符串中真正引用数据库的部分(
提供程序连接字符串
),并将其放入中自己的条目中web.config 的 ;
部分:或者您可以以编程方式执行此操作 - 实体上下文将有一个名为“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: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:
Marc
您可以使用 Elmah.Contrib.EntityFramework nuget 包来实现此目的。
(免责声明:我写的)
You can use Elmah.Contrib.EntityFramework nuget package for this purpose.
(Disclaimer: I wrote it)