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

Move EnableNullabilityCheck from DbContextOptionsBuilder to InMemoryDbContextOptionsBuilde #24528

Merged
merged 1 commit into from
Mar 27, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,48 +134,6 @@ public static DbContextOptionsBuilder UseInMemoryDatabase(
return optionsBuilder;
}

/// <summary>
/// <para>
/// Enables nullability check for all properties across all entities within the in-memory database.
/// </para>
/// </summary>
/// <typeparam name="TContext"> The type of context being configured. </typeparam>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="nullabilityCheckEnabled"> If <see langword="true" />, then nullability check is enforced. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static DbContextOptionsBuilder<TContext> EnableNullabilityCheck<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder,
bool nullabilityCheckEnabled = true)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)EnableNullabilityCheck(
(DbContextOptionsBuilder)optionsBuilder, nullabilityCheckEnabled);

/// <summary>
/// <para>
/// Enables nullability check for all properties across all entities within the in-memory database.
/// </para>
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="nullabilityCheckEnabled"> If <see langword="true" />, then nullability check is enforced. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static DbContextOptionsBuilder EnableNullabilityCheck(
this DbContextOptionsBuilder optionsBuilder,
bool nullabilityCheckEnabled = true)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));

var extension = optionsBuilder.Options.FindExtension<InMemoryOptionsExtension>()
?? new InMemoryOptionsExtension();

extension = extension.WithNullabilityCheckEnabled(nullabilityCheckEnabled);

ConfigureWarnings(optionsBuilder);

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

return optionsBuilder;
}

private static void ConfigureWarnings(DbContextOptionsBuilder optionsBuilder)
{
// Set warnings defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel;
using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Infrastructure
Expand Down Expand Up @@ -36,6 +37,25 @@ public InMemoryDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
/// <returns> The cloned configuration. </returns>
protected virtual DbContextOptionsBuilder OptionsBuilder { get; }

/// <summary>
/// <para>
/// Enables nullability check for all properties across all entities within the in-memory database.
/// </para>
/// </summary>
/// <param name="nullabilityCheckEnabled"> If <see langword="true" />, then nullability check is enforced. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public virtual InMemoryDbContextOptionsBuilder EnableNullabilityCheck(bool nullabilityCheckEnabled = true)
{
var extension = OptionsBuilder.Options.FindExtension<InMemoryOptionsExtension>()
?? new InMemoryOptionsExtension();

extension = extension.WithNullabilityCheckEnabled(nullabilityCheckEnabled);

((IDbContextOptionsBuilderInfrastructure)OptionsBuilder).AddOrUpdateExtension(extension);

return this;
}

#region Hidden System.Object members

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public void IsRequired_for_property_throws_while_inserting_null_value()

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInternalServiceProvider(Fixture.ServiceProvider)
.EnableNullabilityCheck();
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck())
.UseInternalServiceProvider(Fixture.ServiceProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new SomeEntity { Id = 1 });
Expand All @@ -51,9 +50,8 @@ public void IsRequired_for_property_throws_while_inserting_null_value_sensitive(

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck())
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilitySensitiveCheckProvider)
.EnableNullabilityCheck()
.EnableSensitiveDataLogging();

using var context = new DbContext(optionsBuilder.Options);
Expand All @@ -79,9 +77,8 @@ public void IsRequired_for_property_throws_while_inserting_null_value_sensitive_

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck())
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilitySensitiveCheckProvider)
.EnableNullabilityCheck()
.EnableSensitiveDataLogging();

using var context = new DbContext(optionsBuilder.Options);
Expand All @@ -103,9 +100,8 @@ public void RequiredAttribute_for_property_throws_while_inserting_null_value()

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInternalServiceProvider(Fixture.ServiceProvider)
.EnableNullabilityCheck();
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck())
.UseInternalServiceProvider(Fixture.ServiceProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new EntityWithRequiredAttribute { Id = 1 });
Expand All @@ -128,9 +124,8 @@ public void RequiredAttribute_And_IsRequired_for_properties_throws_while_inserti

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInternalServiceProvider(Fixture.ServiceProvider)
.EnableNullabilityCheck();
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck())
.UseInternalServiceProvider(Fixture.ServiceProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new AnotherEntityWithRequiredAttribute { Id = 1 });
Expand All @@ -146,9 +141,8 @@ public void Can_insert_null_value_with_IsRequired_for_property_if_nullability_ch

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider)
.EnableNullabilityCheck(false);
.UseInMemoryDatabase(Guid.NewGuid().ToString(), b => b.EnableNullabilityCheck(false))
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new SomeEntity { Id = 1 });
Expand All @@ -165,9 +159,8 @@ public void Can_insert_null_value_with_RequiredAttribute_for_property_if_nullabi

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider)
.EnableNullabilityCheck(false);
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck(false))
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new EntityWithRequiredAttribute { Id = 1 });
Expand All @@ -184,9 +177,8 @@ public void Can_insert_null_values_with_RequiredAttribute_and_IsRequired_for_pro

var optionsBuilder = new DbContextOptionsBuilder()
.UseModel(modelBuilder.FinalizeModel())
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest))
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider)
.EnableNullabilityCheck(false);
.UseInMemoryDatabase(nameof(NullabilityCheckInMemoryTest), b => b.EnableNullabilityCheck(false))
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider);

using var context = new DbContext(optionsBuilder.Options);
context.Add(new AnotherEntityWithRequiredAttribute { Id = 1 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public virtual void Can_use_self_referencing_overlapping_FK_PK()
var contextOptions = new DbContextOptionsBuilder()
.UseModel(modelBuilder.Model.FinalizeModel())
.UseInternalServiceProvider(InMemoryFixture.DefaultNullabilityCheckProvider)
.UseInMemoryDatabase("Can_use_self_referencing_overlapping_FK_PK")
.EnableNullabilityCheck(false)
.UseInMemoryDatabase("Can_use_self_referencing_overlapping_FK_PK", b => b.EnableNullabilityCheck(false))
.Options;

using (var context = new DbContext(contextOptions))
Expand Down