我有一个从抽象基类继承的上下文类AuditableDbContext : DbContext
。它AuditableDbContext
有两个参数,一个用于审计员,一个用于审计的上下文。
在继承的类中,我有一个默认的无参数构造函数,它使用空参数调用其他构造函数,然后在Database.SetInitializer<MyDbContext>(null)
调用基本构造函数后调用的最终构造函数中。
问题是即使我这样做了,当应用程序启动时,我仍然会在数据库服务器上获得 db 迁移调用。
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);
}
}
我在数据库上看到的查询是两个常见的迁移查询...
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
关于如何阻止实体框架进行这些查询的任何想法?