# Verify.EntityFramework [![Build status](https://ci.appveyor.com/api/projects/status/g6njwv0aox62atu0?svg=true)](https://ci.appveyor.com/project/SimonCropp/verify-entityframework) [![NuGet Status](https://img.shields.io/nuget/v/Verify.EntityFramework.svg)](https://www.nuget.org/packages/Verify.EntityFramework/) [![NuGet Status](https://img.shields.io/nuget/v/Verify.EntityFrameworkClassic.svg)](https://www.nuget.org/packages/Verify.EntityFrameworkClassic/) Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of EntityFramework bits. Support is available via a [Tidelift Subscription](https://tidelift.com/subscription/pkg/nuget-verify?utm_source=nuget-verify&utm_medium=referral&utm_campaign=enterprise).
Part of the .NET Foundation ## Contents * [Usage](#usage) * [EF Core](#ef-core) * [EF Classic](#ef-classic) * [Recording](#recording) * [ChangeTracking](#changetracking) * [Queryable](#queryable) * [EF Core](#ef-core-1) * [EF Classic](#ef-classic-1) * [Security contact information](#security-contact-information) ## NuGet package * https://nuget.org/packages/Verify.EntityFramework/ * https://nuget.org/packages/Verify.EntityFrameworkClassic/ ## Usage Enable VerifyEntityFramewok once at assembly load time: ### EF Core ```cs VerifyEntityFramework.Enable(); ``` snippet source | anchor ### EF Classic ```cs VerifyEntityFrameworkClassic.Enable(); ``` snippet source | anchor ### Recording Recording allows all commands executed by EF to be captured and then (optionally verified). #### Enable Call `VerifyEntityFramework.EnableRecording()` on `DbContextOptionsBuilder`. #### Usage On the `DbContext` call * `VerifyEntityFramework.StartRecording()` to start recording. * `VerifyEntityFramework.FinishRecording()` to finish recording and get the results. ```cs [Test] public async Task Recording() { var database = await DbContextBuilder.GetDatabase("Recording"); var data = database.Context; var company = new Company { Content = "Title" }; data.Add(company); await data.SaveChangesAsync(); data.StartRecording(); await data.Companies .Where(x => x.Content == "Title") .ToListAsync(); var eventData = data.FinishRecording(); await Verifier.Verify(eventData); } ``` snippet source | anchor Will result in the following verified file: ```txt [ { Type: 'Execute', Command: "SELECT [c].[Id], [c].[Content] FROM [Companies] AS [c] WHERE [c].[Content] = N'Title'" } ] ``` snippet source | anchor ### ChangeTracking Added, deleted, and Modified entities can be verified by performing changes on a DbContext and then verifying the instance of ChangeTracking. This approach leverages the [EntityFramework ChangeTracker](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.changetracker). #### Added entity This test: ```cs [Test] public async Task Added() { var options = DbContextOptions(); await using var data = new SampleDbContext(options); var company = new Company { Content = "before" }; data.Add(company); await Verifier.Verify(data.ChangeTracker); } ``` snippet source | anchor Will result in the following verified file: ```txt { Added: { Company: { Id: 0, Content: 'before' } } } ``` snippet source | anchor #### Deleted entity This test: ```cs [Test] public async Task Deleted() { var options = DbContextOptions(); await using var data = new SampleDbContext(options); data.Add(new Company {Content = "before"}); await data.SaveChangesAsync(); var company = data.Companies.Single(); data.Companies.Remove(company); await Verifier.Verify(data.ChangeTracker); } ``` snippet source | anchor Will result in the following verified file: ```txt { Deleted: { Company: { Id: 0 } } } ``` snippet source | anchor #### Modified entity This test: ```cs [Test] public async Task Modified() { var options = DbContextOptions(); await using var data = new SampleDbContext(options); var company = new Company { Content = "before" }; data.Add(company); await data.SaveChangesAsync(); data.Companies.Single().Content = "after"; await Verifier.Verify(data.ChangeTracker); } ``` snippet source | anchor Will result in the following verified file: ```txt { Modified: { Company: { Id: 0, Content: { Original: 'before', Current: 'after' } } } } ``` snippet source | anchor ### Queryable This test: ```cs [Test] public async Task Queryable() { var database = await DbContextBuilder.GetDatabase("Queryable"); var data = database.Context; var queryable = data.Companies .Where(x => x.Content == "value"); await Verifier.Verify(queryable); } ``` snippet source | anchor Will result in the following verified file: ### EF Core ```txt SELECT [c].[Id], [c].[Content] FROM [Companies] AS [c] WHERE [c].[Content] = N'value' ``` snippet source | anchor ### EF Classic ```txt SELECT [Extent1].[Id] AS [Id], [Extent1].[Content] AS [Content] FROM [dbo].[Companies] AS [Extent1] WHERE N'value' = [Extent1].[Content] ``` snippet source | anchor ## Security contact information To report a security vulnerability, use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. ## Icon [Database](https://thenounproject.com/term/database/310841/) designed by [Creative Stall](https://thenounproject.com/creativestall/) from [The Noun Project](https://thenounproject.com/creativepriyanka).