让 MvcMiniProfiler 与 EF4.1 和我们的存储库模式一起使用时遇到问题
我们在尝试让 MvcMiniProfiler 与我们的存储库模式的 EF 实现一起工作时遇到了很多问题。
错误:
实体类型 CustomEntityPewPewFooFoo 不是模型的一部分 当前上下文。
好的。这是我们完成的代码。
自定义 UnitOfWork Context 类
public class UnitOfWork : DbContext
{
public UnitOfWork(string connectionString)
: base(GetProfiledConnection(connectionString))
{ }
private static DbConnection GetProfiledConnection(string connectionString)
{
var parsedConnectionString = new
EntityConnectionStringBuilder(connectionString);
var connection = new
SqlConnection(parsedConnectionString.ProviderConnectionString);
return ProfiledDbConnection.Get(connection);
}
public void Commit() { ... }
}
添加工厂设置内容...
// NOTE: If this is not added, I get an error:
// Unable to find the requested .Net Framework Data Provider.
// It may not be installed.
// In web.config ...
<system.data>
<DbProviderFactories>
<remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
<add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
最后,添加这些额外的内容...
// global.asax, in Application_Start().. because I'm caching
// some database data on startup.
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings[0].ConnectionString);
var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
Database.DefaultConnectionFactory = profiled;
LoadApplicationDataToBeCachedForAllRequests(); // Go and hit the Db! Go Forth!!!
我的连接字符串...
<connectionStrings>
<clear />
<add name="XWing_SqlServer_EF" connectionString="metadata=res://*/SqlServer.XWingModel.csdl|
res://*/SqlServer.XWingModel.ssdl|
res://*/SqlServer.XWingModel.msl;provider=System.Data.SqlClient;
provider connection string='Data Source=Tarantino;Initial Catalog=XWing;Integrated Security=SSPI;MultipleActiveResultSets=True;'"
providerName="System.Data.EntityClient" />
</connectionStrings>
最后(再次),然后是 edmx 信息 ..
- Entity Container Name: XWingEntities
- Namespace: XWing.Repositories.SqlServer
- Filename: XWingModel.edmx
We're having lots of problems trying to get the MvcMiniProfiler to work with our EF implementation of the Repository Pattern.
Error:
The entity type CustomEntityPewPewFooFoo is not part of the model for
the current context.
Ok. this is the code we've done.
Custom UnitOfWork Context class
public class UnitOfWork : DbContext
{
public UnitOfWork(string connectionString)
: base(GetProfiledConnection(connectionString))
{ }
private static DbConnection GetProfiledConnection(string connectionString)
{
var parsedConnectionString = new
EntityConnectionStringBuilder(connectionString);
var connection = new
SqlConnection(parsedConnectionString.ProviderConnectionString);
return ProfiledDbConnection.Get(connection);
}
public void Commit() { ... }
}
Add the factory settings stuff...
// NOTE: If this is not added, I get an error:
// Unable to find the requested .Net Framework Data Provider.
// It may not be installed.
// In web.config ...
<system.data>
<DbProviderFactories>
<remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
<add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
And finally, add this extra stuff....
// global.asax, in Application_Start().. because I'm caching
// some database data on startup.
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings[0].ConnectionString);
var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
Database.DefaultConnectionFactory = profiled;
LoadApplicationDataToBeCachedForAllRequests(); // Go and hit the Db! Go Forth!!!
My connection string...
<connectionStrings>
<clear />
<add name="XWing_SqlServer_EF" connectionString="metadata=res://*/SqlServer.XWingModel.csdl|
res://*/SqlServer.XWingModel.ssdl|
res://*/SqlServer.XWingModel.msl;provider=System.Data.SqlClient;
provider connection string='Data Source=Tarantino;Initial Catalog=XWing;Integrated Security=SSPI;MultipleActiveResultSets=True;'"
providerName="System.Data.EntityClient" />
</connectionStrings>
And finally (again), then edmx info ..
- Entity Container Name: XWingEntities
- Namespace: XWing.Repositories.SqlServer
- Filename: XWingModel.edmx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您遇到的问题与我此处的问题相同。基本上,如果 UnitOfWork 类与 edmx 不在同一程序集中,它将不起作用。
It might be that you have the same kind of problem I had here. Basically, if the UnitOfWork class is not in the same assembly as the edmx, it will not work.