Skip to content

Commit

Permalink
Evolve null-checking approach in SQL Server
Browse files Browse the repository at this point in the history
Part of #19233
  • Loading branch information
ajcvickers committed Nov 4, 2021
1 parent 74f4e5e commit fa6bd22
Show file tree
Hide file tree
Showing 44 changed files with 39 additions and 513 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.SqlServer.Design.Internal
{
Expand Down Expand Up @@ -285,9 +284,6 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
/// </summary>
protected override bool IsHandledByConvention(IModel model, IAnnotation annotation)
{
Check.NotNull(model, nameof(model));
Check.NotNull(annotation, nameof(annotation));

if (annotation.Name == RelationalAnnotationNames.DefaultSchema)
{
return (string?)annotation.Value == "dbo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerDesignTimeServices")]
Expand All @@ -28,9 +27,8 @@ public class SqlServerDesignTimeServices : IDesignTimeServices
/// </summary>
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

serviceCollection.AddEntityFrameworkSqlServer();

#pragma warning disable EF1001 // Internal EF Core API usage.
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
.TryAdd<IAnnotationCodeGenerator, SqlServerAnnotationCodeGenerator>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static DbContextOptionsBuilder UseSqlServer(
this DbContextOptionsBuilder optionsBuilder,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(GetOrCreateExtension(optionsBuilder));

ConfigureWarnings(optionsBuilder);
Expand All @@ -72,7 +70,6 @@ public static DbContextOptionsBuilder UseSqlServer(
string connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));

var extension = (SqlServerOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnectionString(connectionString);
Expand Down Expand Up @@ -107,7 +104,6 @@ public static DbContextOptionsBuilder UseSqlServer(
DbConnection connection,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotNull(connection, nameof(connection));

var extension = (SqlServerOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnection(connection);
Expand Down
11 changes: 0 additions & 11 deletions src/EFCore.SqlServer/Extensions/SqlServerDbSetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
Expand Down Expand Up @@ -40,8 +39,6 @@ public static IQueryable<TEntity> TemporalAsOf<TEntity>(
DateTime utcPointInTime)
where TEntity : class
{
Check.NotNull(source, nameof(source));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
Expand Down Expand Up @@ -88,8 +85,6 @@ public static IQueryable<TEntity> TemporalFromTo<TEntity>(
DateTime utcTo)
where TEntity : class
{
Check.NotNull(source, nameof(source));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
Expand Down Expand Up @@ -137,8 +132,6 @@ public static IQueryable<TEntity> TemporalBetween<TEntity>(
DateTime utcTo)
where TEntity : class
{
Check.NotNull(source, nameof(source));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
Expand Down Expand Up @@ -186,8 +179,6 @@ public static IQueryable<TEntity> TemporalContainedIn<TEntity>(
DateTime utcTo)
where TEntity : class
{
Check.NotNull(source, nameof(source));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
Expand Down Expand Up @@ -219,8 +210,6 @@ public static IQueryable<TEntity> TemporalAll<TEntity>(
this DbSet<TEntity> source)
where TEntity : class
{
Check.NotNull(source, nameof(source));

var queryableSource = (IQueryable)source;
var queryRootExpression = (QueryRootExpression)queryableSource.Expression;
var entityType = queryRootExpression.EntityType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public static EntityTypeBuilder IsMemoryOptimized(
this EntityTypeBuilder entityTypeBuilder,
bool memoryOptimized = true)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

entityTypeBuilder.Metadata.SetIsMemoryOptimized(memoryOptimized);

return entityTypeBuilder;
Expand Down Expand Up @@ -70,8 +68,6 @@ public static OwnedNavigationBuilder IsMemoryOptimized(
this OwnedNavigationBuilder collectionOwnershipBuilder,
bool memoryOptimized = true)
{
Check.NotNull(collectionOwnershipBuilder, nameof(collectionOwnershipBuilder));

collectionOwnershipBuilder.OwnedEntityType.SetIsMemoryOptimized(memoryOptimized);

return collectionOwnershipBuilder;
Expand Down Expand Up @@ -140,11 +136,7 @@ public static bool CanSetIsMemoryOptimized(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool? memoryOptimized,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.MemoryOptimized, memoryOptimized, fromDataAnnotation);
}
=> entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.MemoryOptimized, memoryOptimized, fromDataAnnotation);

/// <summary>
/// Configures the table as temporal.
Expand Down Expand Up @@ -190,11 +182,7 @@ public static bool CanSetIsTemporal(
this IConventionEntityTypeBuilder entityTypeBuilder,
bool temporal = true,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.IsTemporal, temporal, fromDataAnnotation);
}
=> entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.IsTemporal, temporal, fromDataAnnotation);

/// <summary>
/// Configures a history table name for the entity mapped to a temporal table.
Expand Down Expand Up @@ -241,7 +229,6 @@ public static bool CanSetHistoryTableName(
string name,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotNull(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalHistoryTableName, name, fromDataAnnotation);
Expand Down Expand Up @@ -291,11 +278,7 @@ public static bool CanSetHistoryTableSchema(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? schema,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalHistoryTableSchema, schema, fromDataAnnotation);
}
=> entityTypeBuilder.CanSetAnnotation(SqlServerAnnotationNames.TemporalHistoryTableSchema, schema, fromDataAnnotation);

/// <summary>
/// Configures a period start property for the entity mapped to a temporal table.
Expand Down Expand Up @@ -341,12 +324,8 @@ public static bool CanSetPeriodStart(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? propertyName,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(
=> entityTypeBuilder.CanSetAnnotation(
SqlServerAnnotationNames.TemporalPeriodStartPropertyName, propertyName, fromDataAnnotation);
}

/// <summary>
/// Configures a period end property for the entity mapped to a temporal table.
Expand Down Expand Up @@ -392,11 +371,7 @@ public static bool CanSetPeriodEnd(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? propertyName,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(
=> entityTypeBuilder.CanSetAnnotation(
SqlServerAnnotationNames.TemporalPeriodEndPropertyName, propertyName, fromDataAnnotation);
}
}
}
33 changes: 4 additions & 29 deletions src/EFCore.SqlServer/Extensions/SqlServerIndexBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public static class SqlServerIndexBuilderExtensions
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder IsClustered(this IndexBuilder indexBuilder, bool clustered = true)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

indexBuilder.Metadata.SetIsClustered(clustered);

return indexBuilder;
Expand Down Expand Up @@ -106,11 +104,7 @@ public static bool CanSetIsClustered(
this IConventionIndexBuilder indexBuilder,
bool? clustered,
bool fromDataAnnotation = false)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

return indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.Clustered, clustered, fromDataAnnotation);
}
=> indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.Clustered, clustered, fromDataAnnotation);

/// <summary>
/// Configures index include properties when targeting SQL Server.
Expand All @@ -125,7 +119,6 @@ public static bool CanSetIsClustered(
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder IncludeProperties(this IndexBuilder indexBuilder, params string[] propertyNames)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));
Check.NotNull(propertyNames, nameof(propertyNames));

indexBuilder.Metadata.SetIncludeProperties(propertyNames);
Expand All @@ -148,7 +141,6 @@ public static IndexBuilder<TEntity> IncludeProperties<TEntity>(
this IndexBuilder<TEntity> indexBuilder,
params string[] propertyNames)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));
Check.NotNull(propertyNames, nameof(propertyNames));

indexBuilder.Metadata.SetIncludeProperties(propertyNames);
Expand Down Expand Up @@ -180,7 +172,6 @@ public static IndexBuilder<TEntity> IncludeProperties<TEntity>(
this IndexBuilder<TEntity> indexBuilder,
Expression<Func<TEntity, object?>> includeExpression)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));
Check.NotNull(includeExpression, nameof(includeExpression));

IncludeProperties(
Expand Down Expand Up @@ -236,15 +227,11 @@ public static bool CanSetIncludeProperties(
this IConventionIndexBuilder indexBuilder,
IReadOnlyList<string>? propertyNames,
bool fromDataAnnotation = false)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

return (fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention)
=> (fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention)
.Overrides(indexBuilder.Metadata.GetIncludePropertiesConfigurationSource())
|| indexBuilder.Metadata.GetIncludeProperties() is var currentProperties
&& ((propertyNames is null && currentProperties is null)
|| (propertyNames is not null && currentProperties is not null && propertyNames.SequenceEqual(currentProperties)));
}

/// <summary>
/// Configures whether the index is created with online option when targeting SQL Server.
Expand All @@ -259,8 +246,6 @@ public static bool CanSetIncludeProperties(
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder IsCreatedOnline(this IndexBuilder indexBuilder, bool createdOnline = true)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

indexBuilder.Metadata.SetIsCreatedOnline(createdOnline);

return indexBuilder;
Expand Down Expand Up @@ -332,11 +317,7 @@ public static bool CanSetIsCreatedOnline(
this IConventionIndexBuilder indexBuilder,
bool? createdOnline,
bool fromDataAnnotation = false)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

return indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.CreatedOnline, createdOnline, fromDataAnnotation);
}
=> indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.CreatedOnline, createdOnline, fromDataAnnotation);

/// <summary>
/// Configures whether the index is created with fill factor option when targeting SQL Server.
Expand All @@ -351,8 +332,6 @@ public static bool CanSetIsCreatedOnline(
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder HasFillFactor(this IndexBuilder indexBuilder, int fillFactor)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

indexBuilder.Metadata.SetFillFactor(fillFactor);

return indexBuilder;
Expand Down Expand Up @@ -420,10 +399,6 @@ public static bool CanSetFillFactor(
this IConventionIndexBuilder indexBuilder,
int? fillFactor,
bool fromDataAnnotation = false)
{
Check.NotNull(indexBuilder, nameof(indexBuilder));

return indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.FillFactor, fillFactor, fromDataAnnotation);
}
=> indexBuilder.CanSetAnnotation(SqlServerAnnotationNames.FillFactor, fillFactor, fromDataAnnotation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
Expand Down Expand Up @@ -31,8 +30,6 @@ public static class SqlServerKeyBuilderExtensions
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static KeyBuilder IsClustered(this KeyBuilder keyBuilder, bool clustered = true)
{
Check.NotNull(keyBuilder, nameof(keyBuilder));

keyBuilder.Metadata.SetIsClustered(clustered);

return keyBuilder;
Expand Down Expand Up @@ -99,10 +96,6 @@ public static bool CanSetIsClustered(
this IConventionKeyBuilder keyBuilder,
bool? clustered,
bool fromDataAnnotation = false)
{
Check.NotNull(keyBuilder, nameof(keyBuilder));

return keyBuilder.CanSetAnnotation(SqlServerAnnotationNames.Clustered, clustered, fromDataAnnotation);
}
=> keyBuilder.CanSetAnnotation(SqlServerAnnotationNames.Clustered, clustered, fromDataAnnotation);
}
}
Loading

0 comments on commit fa6bd22

Please sign in to comment.