[英]Unable to find database name using EntityCommandExecutionException
In one of my WPF application, I am handling my exceptions at global level. 在我的WPF应用程序之一中,我正在全局范围内处理异常。 I used below code to handle the exception.
我使用下面的代码来处理异常。
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
if (e.Exception is System.Data.Entity.Core.EntityCommandExecutionException)
{
var entityException = e.Exception as System.Data.Entity.Core.EntityCommandExecutionException;
if (entityException.HResult == -2146232004) // Schema is different than expected
{
MessageBox.Show("Database exists but schema is different");
e.Handled = true;
}
}
}
It is working as expected. 它按预期工作。 But I am using multiple DbContext having different connection strings and I want to display the name of database in the error.
但是我正在使用具有不同连接字符串的多个DbContext,并且我想在错误中显示数据库的名称。 I cannot find Connection string in EntityCommandExecutionException.
我在EntityCommandExecutionException中找不到连接字符串。 How to get database name?
如何获得数据库名称?
假设您在错误处理程序中的某个位置可以访问发生错误的DbContext对象,则可以使用dbContextObject.Database.Connection属性访问SqlConnection对象,该对象提供有关数据库连接的各种属性,包括名称数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.