gpt4 book ai didi

c# - Entity Framework SQL First 数据库查询

转载 作者:行者123 更新时间:2023-11-30 16:13:10 25 4
gpt4 key购买 nike

我正在使用代码优先开发 ASP.NET MVC 应用程序。当我从 visual studio 内部运行应用程序时(在调试或 Release模式下),我的第一个查询总是比较慢,因为以下 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 的问题:

  1. 当我将此应用发布到生产环境时,此 SQL 是否仍会运行第一个查询?
  2. 如果是这样,这种情况会在每个用户的第一次查询时发生,还是仅在网站上运行的第一次查询时发生?
  3. 这些查询是否是 EF 工作所必需的,或者有没有办法禁用它们?

最佳答案

除了第一个查询,这是 EF 检查数据库是否仍然匹配概念模型(映射的类模型)。如果存在差异,它将尝试将数据库迁移到最新版本。由于您不想在生产中使用迁移,您可以通过以下方式关闭此功能

Database.SetInitializer<YourContext>(null);

如果您的应用程序在 MVC 中的 global.asax.cs 中进行初始化。

回答你的问题

  1. 是的,它将始终与第一个查询一起运行(除非您如上所述将其关闭)。
  2. 对于应用程序运行的第一个查询,因此当网络应用程序启动时,以及每次重置应用程序池之后。
  3. 如果您想使用迁移,则需要。

关于c# - Entity Framework SQL First 数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22079055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com