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

EfSchemaCompare: Wrong difference reported on default value #3

Closed novar0 closed 6 years ago

novar0 commented 6 years ago

Entity: public class Parameter { [DatabaseGenerated (DatabaseGeneratedOption.Identity)] public int ParameterId { get; set; } public ValueAggregationTypeEnum ValueAggregationTypeId { get; set; } public decimal? NumericValue { get; set; } } public enum ValueAggregationTypeEnum : byte { Invariable = 1, Minimum = 2, Maximum = 3, Average = 4 } protected override void OnModelCreating (ModelBuilder modelBuilder) { modelBuilder.Entity().Property (p => p.ValueAggregationTypeId).HasDefaultValue (ValueAggregationTypeEnum.Invariable); }

SQL (SQL Server 2014): CREATE TABLE [dbo].[Parameters] ( [ParameterId] INT IDENTITY (1, 1) NOT NULL, [NumericValue] DECIMAL (9, 2) NULL, [ValueAggregationTypeId] TINYINT DEFAULT ((1)) NOT NULL, CONSTRAINT [PK_Parameters] PRIMARY KEY CLUSTERED ([ParameterId] ASC) );

CompareEfWithDb returns: DIFFERENT: Parameter->Property 'ValueAggregationTypeId', default value sql. Expected = , found = 1

JonPSmith commented 6 years ago

Hi @novar0,

First, thanks for two very clear issue reports. They were easy to replicate.

I have fixed #3 (silly mistake on my part) but #2 is more complex and I need to ask the EF Core team for help.

I have issued fix for #3 back to the repo, but I haven't created a new NuGet package yet as I would like to get #2 fixed at the same time. I will close this issue once the NuGet package is out.

If you want to continue to use/test EfSchemaCompare then don't forget you can suppress specific errors using the IgnoreTheseErrors config method.

JonPSmith commented 6 years ago

Fixed in version 1.1.3 of NuGet

JonPSmith commented 6 years ago

Hi @novar0,

Just to say that I am updating the EFCore.TestSupport library to .NET Core 2.1 and I found that there is a slight change in the way that EF Core generates a database. I though I would let you know in case this change effects you.

My EfSchemaCompare finds a difference on the SQL default value and it's not a difference I can fix (see error below). You can fix this by casting the enum to byte in the HasDefaultValue.

Here is the difference that EfSchemaCompare finds:

DIFFERENT: Parameter->Property 'ValueAggregationTypeId', default value sql. Expected = Invariable, found = CONVERT([tinyint],(1))