使用mini-Profilier与EF 4.3&安培; MVC 4而不创建数据库 [英] Using Mini-Profilier with EF 4.3 & MVC 4 without creating the database

查看:443
本文介绍了使用mini-Profilier与EF 4.3&安培; MVC 4而不创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里,我们使用EF 4.3 code首先对现有的数据库中的一个问题。我想使用Mini-探查与EF和呼叫

  MvcMiniProfiler.MiniProfilerEF.Initialize();

然而,由于我们实际上并不创造任何表时,DBO .__ MigrationHistory和dbo.EdmMetadata表不存在。探查最终崩溃,因为它们不存在。有没有什么办法让分析器忽略这些EF code首先特定的表?谢谢!

编辑:

这是我得到的例外:(他们来到另发)

 无效的对象名称DBO .__ MigrationHistory。
   在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)
   在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)
   在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,的SqlCommand cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
   在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   在System.Data.SqlClient.SqlDataReader.get_MetaData()
   在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString)
   在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,布尔异步)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串的方法,DbAsyncResult结果)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法)
   在System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行为,串法)
   在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior行为)
   在System.Data.Common.DbCommand.ExecuteReader(的CommandBehavior行为)
   在MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(的CommandBehavior行为)在\\ MVC-迷你探查\\ MvcMiniProfiler \\ DATA \\ ProfiledDbCommand.cs:155线
   在System.Data.Common.DbCommand.ExecuteReader(的CommandBehavior行为)
   在System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,行为的CommandBehavior)无效的对象名称dbo.EdmMetadata。
   在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)
   在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)
   在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,的SqlCommand cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
   在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   在System.Data.SqlClient.SqlDataReader.get_MetaData()
   在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString)
   在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,布尔异步)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串的方法,DbAsyncResult结果)
   在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法)
   在System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行为,串法)
   在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior行为)
   在System.Data.Common.DbCommand.ExecuteReader(的CommandBehavior行为)
   在MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(的CommandBehavior行为)在\\ MVC-迷你探查\\ MvcMiniProfiler \\ DATA \\ ProfiledDbCommand.cs:155线
   在System.Data.Common.DbCommand.ExecuteReader(的CommandBehavior行为)
   在System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,行为的CommandBehavior)


解决方案

我开始了新的MVC 4项目和安装/更新以下的NuGet包:


  • 的EntityFramework

  • MiniProfiler

  • MiniProfiler.EF

我在我的数据库上下文内关闭在code数据库初始化策略第一。

 公共类EmployeeContext:的DbContext
{
    静态EmployeeContext()
    {
        Database.SetInitializer< EmployeeContext>(NULL); //必须迷你分析器运行之前被关闭
    }    公共IDbSet<员工>员工{搞定;组; }
}

迷你探查器是否工作正常。我用手工创建的表数据库。

在静态构造函数关闭数据库初始化是非常重要的。如果你做到这一点在其他地方则有可能是小型探查code你的code之前运行,因此查询到__MigrationHistory表不应该在全部发生。

I have an issue where we are using EF 4.3 Code First against an existing database. I want to use the Mini-Profiler with EF and call

MvcMiniProfiler.MiniProfilerEF.Initialize();

However, since we don't actually create any of the tables, the dbo.__MigrationHistory and dbo.EdmMetadata tables do not exist. The profiler ends up crashing because they don't exist. Is there any way to make the profiler ignore these EF Code First specific tables? Thanks!

EDIT:

These are the exceptions I get: (They come separately)

Invalid object name 'dbo.__MigrationHistory'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in \mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 155
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)



Invalid object name 'dbo.EdmMetadata'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in \mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 155
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

解决方案

I started a new MVC 4 project and installed/updated the following NuGet packages:

  • EntityFramework
  • MiniProfiler
  • MiniProfiler.EF

I turned off the database initialization strategy in Code First inside of my database context.

public class EmployeeContext : DbContext
{
    static EmployeeContext()
    {
        Database.SetInitializer<EmployeeContext>( null ); // must be turned off before mini profiler runs
    }

    public IDbSet<Employee> Employees { get; set; } 
}

The mini profiler is working properly. I created the one table database by hand.

Turning off the database initializer in the static constructor is important. If you do it elsewhere then it's possible that the mini profiler code runs before your code and hence the queries to the __MigrationHistory table that shouldn't be occurring at all.

这篇关于使用mini-Profilier与EF 4.3&安培; MVC 4而不创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆