Database.SetInitializer为null不工作Entity Framework 4.3.1代码优先 [英] Database.SetInitializer to null not working Entity Framework 4.3.1 Code First

查看:465
本文介绍了Database.SetInitializer为null不工作Entity Framework 4.3.1代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上下文类继承自一个抽象基础 AuditableDbContext:DbContext AuditableDbContext 需要两个参数,一个用于审核员,一个用于上下文进行审核。

I have a context class that inherits from an abstract base AuditableDbContext : DbContext. The AuditableDbContext takes two parameters, one for the auditor and one for the context to audit into.

在继承的类中,我有一个默认的无参数构造函数,它调用具有空参数的其他构造函数,然后在最后的构造函数中调用数据库。调用基础构造函数后,SetInitializer< MyDbContext>(null)

In the inherited class, I have a default parameterless constructor that calls the other constructors with null parameters and then in the final constructor I call Database.SetInitializer<MyDbContext>(null) after calling the base constructor.

问题是,即使我这样做,我仍然得到db在应用程序启动时迁移数据库服务器调用。

The problem is that even when I do this, I still get the db migration calls on the database server when the application starts.

public abstract class AuditableContext : DbContext
{
  public AuditableContext(IAuditor auditor, DbContext auditContext)
  {
    // params can be null resulting in no auditing
    // initialization stuff here...
  }
}

public class MyDbContext : AuditableContext
{
  // DbSets here...

  public MyDbContext() : this(null, null) {}

  public MyDbContext(IAuditor auditor) : this(auditor, null) {}

  public MyDbContext(IAuditor auditor, DbContext auditContext) 
  : base(auditor, auditContext)
  {
    Database.SetInitializer<MyDbContext>(null);
  }
}

我在数据库上看到的查询是两个常见的迁移查询...

The queries I see on the database are the two common migration queries...

SELECT [GroupBy1].[A1] AS [C1]
FROM ( SELECT COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
)  AS [GroupBy1]

SELECT TOP (1) 
[Extent1].[Id] AS [Id], 
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC

有关如何阻止Entity Framework进行这些查询的任何想法?

Any ideas on how to stop Entity Framework from making these queries?

推荐答案

您将需要在静态构造函数中执行此操作,或者更好地实例化上下文。

You will need to do this in the static constructor or better still PRIOR to instantiating the Context.

static MyDbContext() {
    Database.SetInitializer<MyDbContext>(null);
}

这篇关于Database.SetInitializer为null不工作Entity Framework 4.3.1代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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