让 MvcMiniProfiler 与 EF4.1 和我们的存储库模式一起使用时遇到问题

发布于 2024-11-30 22:50:12 字数 2666 浏览 3 评论 0原文

我们在尝试让 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 技术交流群。

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

发布评论

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

评论(1

烈酒灼喉 2024-12-07 22:50:12

可能您遇到的问题与我此处的问题相同。基本上,如果 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.

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