.NET

Entity Framework Core Advanced

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

25 면접 질문·
Mid-Level
1

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

답변

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?

답변

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?

답변

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 면접 질문

다음 면접을 위해 .NET을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기