mvc miniprofiler使用DbConnection和实体框架EntityConnection

发布于 2024-11-28 22:44:05 字数 542 浏览 2 评论 0原文

我觉得我在这里缺少一些非常简单的东西,我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2024-12-05 22:44:06

我假设 MyModel 是实体框架设计器创建的上下文,对吧? EntityConnection 可以通过 DbConnection 与 MetadataWorkspace 结合创建。我还假设 EF 设计器已向您的 app/web.config 添加了一个连接字符串属性,其中包含如下内容:

metadata=res://*/Content.MyModel.csdl|res://*/Content.MyModel.ssdl|res://*/Content.MyModel.msl;

这些是组成您的 MetadataWorkspace 的 3 个组件。要根据这些信息创建 EntityConnection,您必须提供数组中的每个文件:

string[] paths =
{
    // "res://" refers to a embedded resource within your DLL, this is automatically done if
    // you've used the EF designer.
    "res://*/Content.MyModel.csdl",
    "res://*/Content.MyModel.ssdl",
    "res://*/Content.MyModel.msl"
};

Assembly[] assembliesToConsider = new Assembly[]
{
    typeof(MyModel).Assembly
};

System.Data.Metadata.Edm.MetadataWorkspace workspace = new System.Data.Metadata.Edm.MetadataWorkspace(paths, assembliesToConsider);
EntityConnection connection = new EntityConnection(workspace, profiledConnection);

我还没有使用 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 a MetadataWorkspace. I also assume that the EF Designer has added a connectionstring-property to your app/web.config which contains something like this:

metadata=res://*/Content.MyModel.csdl|res://*/Content.MyModel.ssdl|res://*/Content.MyModel.msl;

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:

string[] paths =
{
    // "res://" refers to a embedded resource within your DLL, this is automatically done if
    // you've used the EF designer.
    "res://*/Content.MyModel.csdl",
    "res://*/Content.MyModel.ssdl",
    "res://*/Content.MyModel.msl"
};

Assembly[] assembliesToConsider = new Assembly[]
{
    typeof(MyModel).Assembly
};

System.Data.Metadata.Edm.MetadataWorkspace workspace = new System.Data.Metadata.Edm.MetadataWorkspace(paths, assembliesToConsider);
EntityConnection connection = new EntityConnection(workspace, profiledConnection);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文