# Entity Framework Core 応用 (.NET) > Raw SQL、stored procedures、トランザクション、AsNoTracking、AsSplitQuery、query tags、interceptors、パフォーマンスチューニング - 25 面接問題 - Mid-Level - [面接問題: .NET](https://sharpskill.dev/ja/technologies/dotnet/interview-questions.md) ## 1. EF Core でエンティティを取得するために raw SQL クエリを実行するにはどうすればよいですか? **回答** FromSql() または FromSqlRaw() メソッドを使うと、DbContext によって追跡されるエンティティを返しながら raw SQL クエリを実行できます。これは stored procedures やテーブル値関数の呼び出しなど、LINQ ではクエリを直接表現できないシナリオで役立ちます。その後、結果を LINQ と組み合わせてフィルターを追加したり、リレーションを含めたりできます。 ## 2. EF Core における FromSqlRaw() と ExecuteSqlRaw() の違いは何ですか? **回答** FromSqlRaw() は追跡対象のエンティティを返す SELECT クエリの実行に使用され、LINQ との組み合わせが可能です。ExecuteSqlRaw() は非クエリコマンド(INSERT、UPDATE、DELETE)の実行に使用され、影響を受けた行数を返します。FromSqlRaw() は DbSet に適用され、ExecuteSqlRaw() は Database に適用されます。この区別により、型安全性とモデルの整合性が保証されます。 ## 3. EF Core でエンティティを返す stored procedure を呼び出すにはどうすればよいですか? **回答** stored procedure を呼び出して結果をエンティティにマッピングするには、FromSqlRaw() または Database.SqlQuery() を使用します。呼び出しの後に AsEnumerable() を追加することが重要で、これにより EF Core が stored procedure の結果に対して構成を試みて SQL 構文エラーを引き起こすのを防ぎます。パラメーターは SQL インジェクションを防ぐために @p0、@p1 構文を使って渡せます。 ## さらに22問利用可能 - EF Core における AsNoTracking() の役割は何ですか? - AsNoTracking() の代わりに AsNoTrackingWithIdentityResolution() を使うのはどのような場合ですか? 無料で登録: https://sharpskill.dev/ja/login ## その他の.NET面接トピック - [C# の基礎](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/csharp-basics.md): 25問, Junior - [LINQ & Delegates](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/csharp-linq-delegates.md): 20問, Junior - [C#言語の基礎](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/csharp-language-essentials.md): 15問, Junior - [ASP.NET Core の基礎](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/aspnet-core-fundamentals.md): 18問, Junior - [ASP.NET Core のリクエストライフサイクル](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/aspnet-core-request-lifecycle.md): 20問, Junior - [Configuration & Settings](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/configuration-settings.md): 20問, Junior - [アプリケーションのライフサイクル](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/application-lifecycle.md): 20問, Junior - [Dependency Injection](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/dependency-injection.md): 24問, Mid-Level - [Entity Framework Core](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/entity-framework-core.md): 25問, Mid-Level - [Minimal APIs](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/minimal-apis.md): 18問, Mid-Level - [Web API開発](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/web-api-development.md): 22問, Mid-Level - [ASP.NET Core における Async](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/async-aspnet-core.md): 20問, Mid-Level - [Authentication & Authorization](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/authentication-authorization.md): 18問, Mid-Level - [HttpClient & ネットワーキング](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/httpclient-networking.md): 20問, Mid-Level - [JSONシリアライズ](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/json-serialization.md): 20問, Mid-Level - [C# の高度な機能](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/csharp-advanced-features.md): 20問, Mid-Level - [Clean Architecture](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/clean-architecture.md): 20問, Mid-Level - [Logging, Monitoring & Observability](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/logging-monitoring.md): 22問, Mid-Level - [ユニットテストとxUnit](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/unit-testing-xunit.md): 20問, Mid-Level - [統合テスト](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/integration-testing.md): 18問, Mid-Level - [Docker & Containerization](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/docker-containerization.md): 16問, Mid-Level - [NuGet パッケージ管理](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/nuget-package-management.md): 16問, Mid-Level - [Memory Management & GC](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/memory-management-gc.md): 22問, Senior - [リアクティブプログラミング](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/reactive-programming.md): 24問, Senior - [Async/await とパターン](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/async-await-patterns.md): 26問, Senior - [.NET デザインパターン](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/dotnet-design-patterns.md): 24問, Senior - [パフォーマンス最適化](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/performance-optimization.md): 22問, Senior - [セキュリティとベストプラクティス](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/security-best-practices.md): 24問, Senior - [SignalR とリアルタイム](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/signalr-realtime.md): 22問, Senior - [マイクロサービスアーキテクチャ](https://sharpskill.dev/ja/technologies/dotnet/interview-questions/microservices-architecture.md): 25問, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/ja/technologies/dotnet/interview-questions/ef-core-advanced