JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
352 stars 53 forks source link

Support EF Core 5 #37

Closed slipdef closed 4 years ago

slipdef commented 4 years ago

CompareEfWithDb method throws the following exception on EF Core 5 Preview 7: System.InvalidOperationException : Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Diagnostics.IDbContextLogger' while attempting to activate 'Microsoft.EntityFrameworkCore.Internal.DiagnosticsLogger`1[Microsoft.EntityFrameworkCore.DbLoggerCategory+Scaffolding]'. Exception doesn't have a stacktrace

JonPSmith commented 4 years ago

Hi @slipdef,

I won't be updating this library until EF Core 5 is in general release. I am also going to make some changes to this library so it won't be out immediately after the release of EF Core 5.

slipdef commented 4 years ago

Got it

JonPSmith commented 4 years ago

Hi @slipdef,

Sorry, I gave you a quick answer while I was deep into something, and you CAN use CompareEfWithDb in EF Core 5 if you make sure the correct NuGet packages are added. Here is what I have in my example code for the second edition of my book which covers EF Core 5.

  <ItemGroup>
    <PackageReference Include="EfCore.TestSupport" Version="3.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0-preview.7.20365.15">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    ... other test parts
  </ItemGroup>

I think the important ones are:

slipdef commented 4 years ago

Thanks, adding Microsoft.EntityFrameworkCore.Design helped but now CompareEfWithDb always thinks that the schema and DbContext have differencies (from a quick look the cause is owned props). Not sure if it makes sense to go deeper until you confirm that TestSupport 3.2 should work fine with EF Core 5.

JonPSmith commented 4 years ago

Hi @slipdef,

There is a bug CompareEfWithDb around owned types: in EF Core 3 non-nullable properties in an owned type are saved as nullable. I haven't fixed this bug. The code is rather complex because of supporting both EF Core 2.x and EF Core 3.x. I don't plan to fix this until I get to EF Core 5.

You can add an ignore for these errors via the IgnoreTheseErrors or AddIgnoreCompareLog.

JonPSmith commented 3 years ago

Hi @slipdef,

Just to say I have split the EfSchemaCompare out into its own project and added a lots of features/fixes, especially around EF Core 5 new features.

There is also EF Core 5 version of EfCore.TestSupport out now, but I recommend you read the Version 5 upgrade doc as there are a lot of changes, some of which are breaking changes.