EF6 continually Access__MigrationHistory table leading to SQL errors

ps-rizwan 21 Reputation points
2023-04-27T10:31:45.88+00:00

Customers have reported slowness in the our application and have seen a large number of errors reported in SQL. The hosted environment’s performance logging tool (NewRelic) has reported an increase in failure rate which the customer suspect is the cause for application slowness. Application uses Entity Framework 6 for data access.

The application uses Entity First Database first approach to access the database. We are seeing the below queries are continuously getting executed on the database:

SELECT Count(*) FROM sys.databases WHERE [name]=N?

SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_SCHEMA + ? + t.TABLE_NAME IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) OR t.TABLE_NAME = ?

SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(?) AS [A1] FROM [dbo].[__MigrationHistory] AS [Extent1] WHERE [Extent1].[ContextKey] = @p__linq__0 ) AS [GroupBy1]

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

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

When we checked on the database, we are not able to find the “__MigrationHistory” table neither “EdmMetadata” table.

How should we go about stopping these queries for our application?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,441 questions
{count} votes

Accepted answer
  1. Vahid Ghafarpour 18,685 Reputation points
    2023-04-28T04:32:01.9+00:00

    It seems like your application is continuously executing certain queries that are not required and may be causing performance issues. Here are a few steps you can take to stop these queries:

    Check if the Entity Framework context is configured correctly: If the __MigrationHistory and EdmMetadata tables are not present in the database, it is possible that the Entity Framework context is not configured correctly. Check if the database context class is inheriting from the DbContext class and if the migration configuration class is properly set up.

    Disable automatic migrations: If you are not using Entity Framework Migrations, you can disable automatic migrations by setting the "AutomaticMigrationsEnabled" property to "false" in the migration configuration class.

    Optimize your Entity Framework queries: If you are using Entity Framework queries in your application, make sure that they are optimized for performance. You can use the Entity Framework Profiler or SQL Server Profiler to identify any inefficient queries.

    Cache frequently accessed data: If certain data is frequently accessed, consider caching it in memory to reduce the number of database queries.

    Tune your database: If your database is experiencing performance issues, consider tuning it by optimizing indexes, updating statistics, and monitoring database performance metrics.

    Use a performance monitoring tool: Use a performance monitoring tool like SQL Server Profiler or New Relic to identify the root cause of the performance issues and to monitor the impact of any changes made to the application or database.

    By taking these steps, you should be able to identify and stop the unnecessary queries that are causing performance issues in your application.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful