Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User
SQL ;

SQL Entity Unterschied .FindAsync(ID) und .FirstOrDefaultAsync(x= x.IDArticle==id);

06.02.2023 (👁3347)

SQL Entity Unterschied:

Was ist der Unterschied der beiden Aufrufe eines Datensatzes in C# Entity Framework

Bei der Übersetzung zur Datenbank Abfrage in SQL Server

Abfrage 1:  FindAsync(ID)

//*get matching Article from Database by ID

var dbArticle = await _dbContext.tbl_Articles.FindAsync(id);

//*SELECT TOP(1) IDArticle,Title FROM tbl_Articles AS t WHERE IDArticle = @p_0

 

Abfrage 2:  FirstOrDefaultAsync(x=>x.IDArticle==id);

_dbContext.tbl_Articles.FirstOrDefaultAsync(x=>x.IDArticle==id);

//*SELECT TOP(1) IDArticle,Title FROM tbl_Articles AS t WHERE IDArticle = @p_0

//*first db call ~43ms second call ~5ms

 

Beide SQL Abfragen folgen wie hier

SELECT TOP(1) [t].[IDArticle], [t].[Title]

      FROM [tbl_Articles] AS [t]

      WHERE [t].[IDArticle] = @__p_0

Fazit:

Beide EF Abfragen erfolgen in einer DBCommand SQL Abfrage mit SELECT TOP(1)

Und sind somit identisch  in der Geschwindigkeit

Ein Bild, das Text, Screenshot, Monitor, Bildschirm enthält.

Automatisch generierte Beschreibung

info: Microsoft.EntityFrameworkCore.Database.Command[20101]

      Executed DbCommand (42ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']

      SELECT TOP(1) [t].[IDArticle], [t].[Title]

      FROM [tbl_Articles] AS [t]

      WHERE [t].[IDArticle] = @__p_0

info: Microsoft.EntityFrameworkCore.Database.Command[20101]

      Executed DbCommand (5ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']

      SELECT TOP(1) [t].[IDArticle], [t].[Title]

      FROM [tbl_Articles] AS [t]

      WHERE [t].[IDArticle] = @__p_0