Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to EF Core 6.0 #57

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup .NET Core SDK
uses: actions/[email protected]
with:
dotnet-version: '5.0.x'
dotnet-version: '6.0.100'

- name: Test
run: dotnet test
Expand Down
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<AnalysisLevel>latest</AnalysisLevel>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>9.0</LangVersion>
<LangVersion>latest</LangVersion>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project>
<PropertyGroup>
<EFCoreVersion>5.0.12</EFCoreVersion>
<EFCoreVersion>6.0.0</EFCoreVersion>
<MicrosoftExtensionsVersion>6.0.0</MicrosoftExtensionsVersion>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="$(EFCoreVersion)" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EFCoreVersion)" />

Expand Down
25 changes: 13 additions & 12 deletions EFCore.CheckConstraints.Test/DiscriminatorCheckConstraintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using EFCore.CheckConstraints.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -29,9 +29,9 @@ public void Generate_check_constraint_with_all_enum_names()
});
});

var checkConstraint = Assert.Single(model.FindEntityType("Parent").GetCheckConstraints());
var checkConstraint = Assert.Single(model.FindEntityType("Parent")!.GetCheckConstraints());
Assert.NotNull(checkConstraint);
Assert.Equal("CK_Parent_Discriminator", checkConstraint.Name);
Assert.Equal("CK_Parent_Discriminator", checkConstraint!.Name);
Assert.Equal("[Discriminator] IN (N'Child', N'Parent')", checkConstraint.Sql);
}

Expand All @@ -45,7 +45,7 @@ public void Generate_check_constraint_skips_abstract_types()
b.Entity<Derived>();
});

var checkConstraint = Assert.Single(model.FindEntityType(typeof(Base)).GetCheckConstraints());
var checkConstraint = Assert.Single(model.FindEntityType(typeof(Base))!.GetCheckConstraints());
Assert.NotNull(checkConstraint);
Assert.Equal("CK_Base_Discriminator", checkConstraint.Name);
Assert.Equal("[Discriminator] IN (N'Base', N'Derived')", checkConstraint.Sql);
Expand All @@ -59,28 +59,29 @@ private class Base

private abstract class Intermediate : Base
{
public string Value { get; set; }
public string Value { get; set; } = null!;
}

private class Derived : Intermediate
{
public string Property { get; set; }
public string Property { get; set; } = null!;
}

#region Support

private IModel BuildModel(Action<ModelBuilder> buildAction)
{
var serviceProvider = SqlServerTestHelpers.Instance.CreateContextServices();
var conventionSet = serviceProvider.GetRequiredService<IConventionSetBuilder>().CreateConventionSet();

var conventionSet = SqlServerTestHelpers.Instance.CreateConventionSetBuilder().CreateConventionSet();
ConventionSet.Remove(conventionSet.ModelFinalizedConventions, typeof(ValidatingConvention));
conventionSet.ModelFinalizingConventions.Add(
new DiscriminatorCheckConstraintConvention(serviceProvider.GetRequiredService<ISqlGenerationHelper>()));
new DiscriminatorCheckConstraintConvention(
serviceProvider.GetRequiredService<IRelationalTypeMappingSource>(),
serviceProvider.GetRequiredService<ISqlGenerationHelper>()));

var builder = new ModelBuilder(conventionSet);
buildAction(builder);
return builder.FinalizeModel();
var modelBuilder = new ModelBuilder(conventionSet);
buildAction(modelBuilder);
return modelBuilder.FinalizeModel();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
29 changes: 16 additions & 13 deletions EFCore.CheckConstraints.Test/EnumCheckConstraintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -96,13 +97,13 @@ public void TPH()
});
});

var customerCheckConstraint = Assert.Single(model.FindEntityType("Customer").GetCheckConstraints());
var customerCheckConstraint = Assert.Single(model.FindEntityType("Customer")!.GetCheckConstraints());
Assert.NotNull(customerCheckConstraint);
Assert.Equal("CK_Customer_Type_Enum", customerCheckConstraint.Name);
Assert.Equal("CK_Customer_Type_Enum", customerCheckConstraint!.Name);

var specialCustomerCheckConstraint = Assert.Single(model.FindEntityType("SpecialCustomer").GetCheckConstraints());
Assert.NotNull(specialCustomerCheckConstraint);
Assert.Equal("CK_Customer_AnotherType_Enum", specialCustomerCheckConstraint.Name);
Assert.Collection(model.FindEntityType("SpecialCustomer")!.GetCheckConstraints().OrderBy(ck => ck.Name),
ck => Assert.Equal("CK_Customer_AnotherType_Enum", ck.Name),
ck => Assert.Same(customerCheckConstraint, ck));
}

[Fact]
Expand All @@ -123,13 +124,14 @@ public void TPT()
});
});

var customerCheckConstraint = Assert.Single(model.FindEntityType("Customer").GetCheckConstraints());
var customerCheckConstraint = Assert.Single(model.FindEntityType("Customer")!.GetCheckConstraints());
Assert.NotNull(customerCheckConstraint);
Assert.Equal("CK_Customer_Type_Enum", customerCheckConstraint.Name);
Assert.Equal("CK_Customer_Type_Enum", customerCheckConstraint!.Name);

var specialCustomerCheckConstraint = Assert.Single(model.FindEntityType("SpecialCustomer").GetCheckConstraints());
Assert.NotNull(specialCustomerCheckConstraint);
Assert.Equal("CK_SpecialCustomer_AnotherType_Enum", specialCustomerCheckConstraint.Name);
Assert.Collection(
model.FindEntityType("SpecialCustomer")!.GetCheckConstraints().OrderBy(ck => ck.Name),
ck => Assert.Same(customerCheckConstraint, ck),
ck => Assert.Equal("CK_SpecialCustomer_AnotherType_Enum", ck.Name));
}

#region Support
Expand All @@ -152,11 +154,12 @@ private enum FlagsEnum
private IModel BuildModel(Action<ModelBuilder> buildAction)
{
var serviceProvider = SqlServerTestHelpers.Instance.CreateContextServices();
var conventionSet = serviceProvider.GetRequiredService<IConventionSetBuilder>().CreateConventionSet();

var conventionSet = SqlServerTestHelpers.Instance.CreateConventionSetBuilder().CreateConventionSet();
ConventionSet.Remove(conventionSet.ModelFinalizedConventions, typeof(ValidatingConvention));
conventionSet.ModelFinalizingConventions.Add(
new EnumCheckConstraintConvention(serviceProvider.GetRequiredService<ISqlGenerationHelper>()));
new EnumCheckConstraintConvention(
serviceProvider.GetRequiredService<IRelationalTypeMappingSource>(),
serviceProvider.GetRequiredService<ISqlGenerationHelper>()));

var builder = new ModelBuilder(conventionSet);
buildAction(builder);
Expand Down
24 changes: 14 additions & 10 deletions EFCore.CheckConstraints.Test/OptionsBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void DiscriminatorCheckConstraintOptionsTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.True(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options.AreEnumCheckConstraintsEnabled);
Assert.False(options.AreValidationCheckConstraintsEnabled);
Assert.Equal("using check constraints (discriminators)", options.Info.LogFragment);
Expand All @@ -42,7 +42,7 @@ public void EnumCheckConstraintOptionsTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.False(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options.AreEnumCheckConstraintsEnabled);
Assert.False(options.AreValidationCheckConstraintsEnabled);
Assert.Equal("using check constraints (enums)", options.Info.LogFragment);
Expand All @@ -63,15 +63,16 @@ public void ValidationCheckConstraintOptionsTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.False(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options.AreEnumCheckConstraintsEnabled);
Assert.True(options.AreValidationCheckConstraintsEnabled);
Assert.Equal("using check constraints (validation)", options.Info.LogFragment);
Assert.NotNull(options.ValidationCheckConstraintOptions);

var validationOptions = options.ValidationCheckConstraintOptions;

Assert.Null(validationOptions.CreditCardRegex);
Assert.NotNull(validationOptions);
Assert.Null(validationOptions!.CreditCardRegex);
Assert.Null(validationOptions.EmailAddressRegex);
Assert.Null(validationOptions.PhoneRegex);
Assert.Null(validationOptions.UrlRegex);
Expand All @@ -92,15 +93,16 @@ public void ValidationCheckConstraintOptionsNoRegexTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.False(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.False(options.AreEnumCheckConstraintsEnabled);
Assert.True(options.AreValidationCheckConstraintsEnabled);
Assert.Equal("using check constraints (validation)", options.Info.LogFragment);
Assert.NotNull(options.ValidationCheckConstraintOptions);

var validationOptions = options.ValidationCheckConstraintOptions;

Assert.Null(validationOptions.CreditCardRegex);
Assert.NotNull(validationOptions);
Assert.Null(validationOptions!.CreditCardRegex);
Assert.Null(validationOptions.EmailAddressRegex);
Assert.Null(validationOptions.PhoneRegex);
Assert.Null(validationOptions.UrlRegex);
Expand All @@ -121,14 +123,15 @@ public void AllCheckConstraintOptionsTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.True(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options.AreEnumCheckConstraintsEnabled);
Assert.True(options.AreValidationCheckConstraintsEnabled);
Assert.NotNull(options.ValidationCheckConstraintOptions);

var validationOptions = options.ValidationCheckConstraintOptions;

Assert.Null(validationOptions.CreditCardRegex);
Assert.NotNull(validationOptions);
Assert.Null(validationOptions!.CreditCardRegex);
Assert.Null(validationOptions.EmailAddressRegex);
Assert.Null(validationOptions.PhoneRegex);
Assert.Null(validationOptions.UrlRegex);
Expand All @@ -149,14 +152,15 @@ public void AllCheckConstraintOptionsNoRegexTest()
var options = extensions[0] as CheckConstraintsOptionsExtension;

Assert.NotNull(options);
Assert.True(options.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options!.AreDiscriminatorCheckConstraintsEnabled);
Assert.True(options.AreEnumCheckConstraintsEnabled);
Assert.True(options.AreValidationCheckConstraintsEnabled);
Assert.NotNull(options.ValidationCheckConstraintOptions);

var validationOptions = options.ValidationCheckConstraintOptions;

Assert.Null(validationOptions.CreditCardRegex);
Assert.NotNull(validationOptions);
Assert.Null(validationOptions!.CreditCardRegex);
Assert.Null(validationOptions.EmailAddressRegex);
Assert.Null(validationOptions.PhoneRegex);
Assert.Null(validationOptions.UrlRegex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected SqlServerTestHelpers()
{
}

public static SqlServerTestHelpers Instance { get; } = new SqlServerTestHelpers();
public static SqlServerTestHelpers Instance { get; } = new();

public override IServiceCollection AddProviderServices(IServiceCollection services)
=> services.AddEntityFrameworkSqlServer();
Expand Down
31 changes: 20 additions & 11 deletions EFCore.CheckConstraints.Test/ValidationCheckConstraintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -117,34 +118,42 @@ public virtual void RegularExpression()
class Blog
{
public int Id { get; set; }

[Range(1, 5)]
public int Rating { get; set; }

[MinLength(4)]
public string Name { get; set; }
public string Name { get; set; } = null!;

[StringLength(100, MinimumLength = 1)]
public string Required { get; set; }
public string Required { get; set; } = null!;

[Phone]
public string PhoneNumber { get; set; }
public string PhoneNumber { get; set; } = null!;

[CreditCard]
public string CreditCard { get; set; }
public string CreditCard { get; set; } = null!;

[EmailAddress]
public string Email { get; set; }
public string Email { get; set; } = null!;

[Url]
public string Address { get; set; }
public string Address { get; set; } = null!;

[RegularExpression("^A")]
public string StartsWithA { get; set; }
public string StartsWithA { get; set; } = null!;
}
// ReSharper restore UnusedMember.Local

private IModel BuildModel(Action<ModelBuilder> buildAction, bool useRegex)
{
var serviceProvider = SqlServerTestHelpers.Instance.CreateContextServices();
var conventionSet = serviceProvider.GetRequiredService<IConventionSetBuilder>().CreateConventionSet();

var conventionSet = SqlServerTestHelpers.Instance.CreateConventionSetBuilder().CreateConventionSet();
ConventionSet.Remove(conventionSet.ModelFinalizedConventions, typeof(ValidatingConvention));
conventionSet.ModelFinalizingConventions.Add(
new ValidationCheckConstraintConvention(
new ValidationCheckConstraintOptions() { UseRegex = useRegex },
new ValidationCheckConstraintOptions { UseRegex = useRegex },
serviceProvider.GetRequiredService<IRelationalTypeMappingSource>(),
serviceProvider.GetRequiredService<ISqlGenerationHelper>(),
serviceProvider.GetRequiredService<IRelationalTypeMappingSource>(),
serviceProvider.GetRequiredService<IDatabaseProvider>()));
Expand All @@ -154,7 +163,7 @@ private IModel BuildModel(Action<ModelBuilder> buildAction, bool useRegex)
return builder.FinalizeModel();
}

private IEntityType BuildEntityType<TEntity>(Action<EntityTypeBuilder<TEntity>> buildAction = null, bool useRegex = true)
private IEntityType BuildEntityType<TEntity>(Action<EntityTypeBuilder<TEntity>>? buildAction = null, bool useRegex = true)
where TEntity : class
{
return BuildModel(buildAction is null
Expand Down
Loading