.NET

Entity Framework Core Advanced

Raw SQL, stored procedures, transactions, AsNoTracking, AsSplitQuery, query tags, interceptors, performance tuning

25 domande da colloquioยท
Mid-Level
1

How to execute a raw SQL query to retrieve entities in EF Core?

Risposta

The FromSql() or FromSqlRaw() method allows executing raw SQL queries while returning entities tracked by the DbContext. This is useful for scenarios where LINQ cannot express the query directly, such as calling stored procedures or table-valued functions. Results can then be composed with LINQ to add filters or include relationships.

2

What is the difference between FromSqlRaw() and ExecuteSqlRaw() in EF Core?

Risposta

FromSqlRaw() is used to execute SELECT queries that return tracked entities, allowing composition with LINQ. ExecuteSqlRaw() is used to execute non-query commands (INSERT, UPDATE, DELETE) and returns the number of affected rows. FromSqlRaw() applies to a DbSet, while ExecuteSqlRaw() applies to Database. This distinction ensures type safety and model consistency.

3

How to call a stored procedure that returns entities in EF Core?

Risposta

Use FromSqlRaw() or Database.SqlQuery<T>() to call a stored procedure and map results to entities. It's crucial to add AsEnumerable() after the call to prevent EF Core from attempting to compose on the stored procedure results, which would cause SQL syntax errors. Parameters can be passed using the @p0, @p1 syntax to prevent SQL injection.

4

What is the role of AsNoTracking() in EF Core?

5

When to use AsNoTrackingWithIdentityResolution() instead of AsNoTracking()?

+22 domande da colloquio

Padroneggia .NET per il tuo prossimo colloquio

Accedi a tutte le domande, flashcards, test tecnici, esercizi di code review e simulatori di colloquio.

Inizia gratis