我首先使用代码处理ASP.NET MVC应用程序。当我从visual studio内部运行应用程序时(在调试或发布模式下),由于以下自动运行的SQL,我的第一个查询总是较慢:
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_TYPE = 'BASE TABLE' AND
(t.TABLE_SCHEMA + '.' + t.TABLE_NAME IN
('dbo.LoginAttempt','dbo.Product','dbo.Supplier','dbo.AspNetRoles',
'dbo.AspNetUsers','dbo.AspNetUserClaims','dbo.AspNetUserLogins','dbo.AspNetUserRoles')
OR t.TABLE_NAME = 'EdmMetadata') -- Executing at 2/27/2014 2:43:58 PM -05:00 -- Completed in 20 ms with result: 8
SELECT [GroupBy1].[A1] AS [C1] FROM (
SELECT COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
) AS [GroupBy1] -- Executing at 2/27/2014 2:43:59 PM -05:00 -- Completed in 3 ms with result: SqlDataReader
SELECT [GroupBy1].[A1] AS [C1] FROM (
SELECT COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE ([Extent1].[ContextKey] = @p__linq__0)
AND (@p__linq__0 IS NOT NULL) ) AS [GroupBy1] -- p__linq__0: 'CompanyName.Migrations.Configuration' (Type = String, Size = 4000) -- Executing at 2/27/2014 2:43:59 PM -05:00 -- Completed in 2 ms with result: SqlDataReader
SELECT TOP (1) [Project1].[C1] AS [C1], [Project1].[MigrationId] AS [MigrationId],
[Project1].[Model] AS [Model] FROM (
SELECT [Extent1].[MigrationId] AS [MigrationId],
[Extent1].[Model] AS [Model], 1 AS [C1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE ([Extent1].[ContextKey] = @p__linq__0)
AND (@p__linq__0 IS NOT NULL) ) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC -- p__linq__0: 'CompanyName.Migrations.Configuration' (Type = String, Size = 4000) -- Executing at 2/27/2014 2:43:59 PM -05:00 -- Completed in 2 ms with result: SqlDataReader
我对此SQL有几个问题:
答案 0 :(得分:3)
除第一个查询外,这是EF检查数据库是否仍然与概念模型(映射的类模型)匹配。如果存在差异,它将尝试将数据库迁移到最新版本。由于您不想在生产中使用迁移,因此可以通过
关闭它Database.SetInitializer<YourContext>(null);
在初始化中,如果你的应用程序,global.asax.cs
在MVC中。
回答你的问题