mvc miniprofiler使用DbConnection和实体框架EntityConnection
我觉得我在这里缺少一些非常简单的东西,我正在将 MVC Miniprofiler 集成到我的项目中,但我找不到使用正确连接实例化我的上下文的方法,因为它不是 EF Code First 并且我的模型继承自 ObjectContext,在这里我的代码
public static MyModel GetDataBaseModel()
{
DbConnection conn = new MySqlConnection(Utils.GetConnectionString());
// this returns a DBConnection
DbConnection profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(conn);
//my model needs an EntityConnection, so this is not possible
return new MyModel(profiledConnection);
}
如何解决这个问题?
I feel I am missing something really simple here, I am integrating MVC Miniprofiler into my project but I can't find the way to instantiate my context with the right connection, because it is not EF Code First and my model inherits from ObjectContext, here is my code
public static MyModel GetDataBaseModel()
{
DbConnection conn = new MySqlConnection(Utils.GetConnectionString());
// this returns a DBConnection
DbConnection profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(conn);
//my model needs an EntityConnection, so this is not possible
return new MyModel(profiledConnection);
}
How to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设 MyModel 是实体框架设计器创建的上下文,对吧? EntityConnection 可以通过 DbConnection 与
MetadataWorkspace
结合创建。我还假设 EF 设计器已向您的 app/web.config 添加了一个连接字符串属性,其中包含如下内容:这些是组成您的 MetadataWorkspace 的 3 个组件。要根据这些信息创建 EntityConnection,您必须提供数组中的每个文件:
我还没有使用 MvcMiniProfiler 对此进行测试(现在首先安装它),但如果配置文件连接不完全正确,则可能会出现一些问题行为类似于原始的 MySqlConnection,请尝试一下,否则将尝试创建您的设置。
I assume that
MyModel
is the context created by the Entity Framework Designer, right? An EntityConnection can be created from a DbConnection in combination with aMetadataWorkspace
. I also assume that the EF Designer has added a connectionstring-property to your app/web.config which contains something like this:These are the 3 components that make up your MetadataWorkspace. To create your an EntityConnection from these information you must provide each file within an array:
I haven't tested this with the MvcMiniProfiler (first installed it right now), but there might be some issues, if the profiled-connection doesn't exactly behave like the original MySqlConnection, give a try otherwise a will try to create your setup.