我在用mvc-迷你剖析器 MVC 3和实体框架代码构建的项目中使用ASP.Net。
在我尝试通过将连接包装在ProfiledDbConnection中来添加数据库概要之前,一切都很好,如文档中所述。由于我使用的是DbContext,所以我试图提供连接的方式是通过构造函数使用静态工厂方法:
public class MyDbContext : DbContext
{
public MyDbContext() : base(GetProfilerConnection(), true)
{ }
private static DbConnection GetProfilerConnection()
{
// Code below errors
//return ProfiledDbConnection.Get(new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString));
// Code below works fine...
return new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString);
}
//...
}当使用ProfiledDbConnection时,我得到以下错误:
ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.
堆栈跟踪:
[ArgumentException: The connection is not of type 'System.Data.SqlClient.SqlConnection'.]
System.Data.SqlClient.SqlProviderUtilities.GetRequiredSqlConnection(DbConnection connection) +10486148
System.Data.SqlClient.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +77
System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +44
[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +11092901
System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +11092745
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +221
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +61
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +1203482
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +492
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +26
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +89
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +44
System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +135我已经完成了一步,ProfiledDbConnection.Get返回的类型是ProfiledDbConnection类型(即使当前的MiniProfiler为null)。
在实例化MiniProfiler.Start()之前,在全局Application_BeginRequest()方法中调用DbContext方法。我也会为每个请求调用Start方法,而不考虑任何情况,但如果用户没有处于正确的角色中,则调用stop:
protected void Application_BeginRequest()
{
// We don't know who the user is at this stage so need to start for everyone
MiniProfiler.Start();
}
protected void Application_AuthorizeRequest(Object sender, EventArgs e)
{
// Now stop the profiler if the user is not a developer
if (!AuthorisationHelper.IsDeveloper())
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
}
protected void Application_EndRequest()
{
MiniProfiler.Stop();
}我不确定这是否会影响到一些事情,但是我也使用StructureMap作为IoC来表示DbContext,使用下面的初始值:
For<MyDbContext>().Singleton().HybridHttpOrThreadLocalScoped();我知道这里有一个类似的问题,很好地解释了这个用户正在发生的事情,但是它似乎并没有解决我的问题。
编辑:
为了清晰。我试图将连接作为ProfiledDbConnection传递,以便首先分析从实体框架代码生成的sql。

实体框架期望使用SqlConnection类型的连接,这当然不是。
下面是我的连接字符串的一个示例(请注意providerName)
<add name="MyDbContext" connectionString="Server=.\SQLEXPRESS; Database=MyDatabase;Trusted_Connection=true;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />我试图创建自己版本的ProfiledDbConnection,继承自SqlConnection,但它是一个密封类。
如果有某种方式告诉实体框架有关自定义连接类型,那么这可能会奏效。我尝试将连接字符串中的providerName设置为MvcMiniProfiler.Data.ProfiledDbConnection,但这没有起作用。
所以。也许问题的一个演变是:如何首先将自定义连接类型传递给实体框架代码?
发布于 2011-07-19 07:56:46
现在完全支持这一点,查看最新的源代码或从nuget获取包。
如果您正在使用nuget,您将需要MiniProfiler.EF包。(1.9.1及以上)
支持这一点需要对底层代理对象进行大量修改,以支持充当EF代码第一代理。
为了增加这种支持:
在Application_Start运行期间:
MiniProfilerEF.Initialize();
备注:EF代码首先将表元数据存储在一个名为:EdmMetadata的表中。此元数据将提供程序用作实体键的一部分。如果将提供程序初始化为非剖析提供程序,则必须重新构建此元数据。从EdmMetadata中删除所有行可能会起到这个作用,或者一些更聪明的提供者能够透明地处理这个问题。
发布于 2011-07-23 13:53:50
我仍然无法让它工作,并发现我需要重命名或删除连接字符串,以使Database.DefaultConnectionFactory工作。
发布于 2011-07-12 21:02:31
在我的经验中,这个错误一直是无效的连接字符串,或者缺少到DB的连接,比如“连接时发生了网络服务错误.”。
还请注意,DbContext只需要构造函数中的"connectionStringKey“,如
public MyDbContext() :
base("MyConnectionName", true)
{ }https://stackoverflow.com/questions/6550046
复制相似问题