开发者

Trouble getting the MvcMiniProfiler to work with EF4.1 and our Repository Pattern

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜