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

EFCore: Enable nullable on the project #24265

Merged
1 commit merged into from
Feb 26, 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
4 changes: 2 additions & 2 deletions src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public virtual void ProcessModelFinalizing(

if (_options.UseLazyLoadingProxies)
{
if (!navigationBase.PropertyInfo.GetMethod.IsReallyVirtual()
if (!navigationBase.PropertyInfo.GetMethod!.IsReallyVirtual()
&& (!(navigationBase is INavigation navigation
&& navigation.ForeignKey.IsOwnership)))
{
Expand All @@ -144,7 +144,7 @@ public virtual void ProcessModelFinalizing(
{
indexerChecked = true;

if (!property.PropertyInfo!.SetMethod.IsReallyVirtual())
if (!property.PropertyInfo!.SetMethod!.IsReallyVirtual())
{
if (clrType.IsGenericType
&& clrType.GetGenericTypeDefinition() == typeof(Dictionary<,>)
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/ITable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface ITable : ITableBase
/// </summary>
IEnumerable<ICheckConstraint> CheckConstraints
=> EntityTypeMappings.SelectMany(m => CheckConstraint.GetCheckConstraints(m.EntityType))
.Distinct((x, y) => x.Name == y.Name);
.Distinct((x, y) => x!.Name == y!.Name);
smitpatel marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the comment for this table.
Expand Down
9 changes: 5 additions & 4 deletions src/EFCore/ChangeTracking/EntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public virtual bool IsKeySet
/// <value> The current values. </value>
public virtual PropertyValues CurrentValues
{
[DebuggerStepThrough] get => new CurrentPropertyValues(InternalEntry);
[DebuggerStepThrough]
get => new CurrentPropertyValues(InternalEntry);
}

/// <summary>
Expand All @@ -333,7 +334,8 @@ public virtual PropertyValues CurrentValues
/// <value> The original values. </value>
public virtual PropertyValues OriginalValues
{
[DebuggerStepThrough] get => new OriginalPropertyValues(InternalEntry);
[DebuggerStepThrough]
get => new OriginalPropertyValues(InternalEntry);
}

/// <summary>
Expand Down Expand Up @@ -376,8 +378,7 @@ public virtual PropertyValues OriginalValues
/// <exception cref="OperationCanceledException"> If the <see cref="CancellationToken"/> is canceled. </exception>
public virtual async Task<PropertyValues?> GetDatabaseValuesAsync(CancellationToken cancellationToken = default)
{
var values = await Finder.GetDatabaseValuesAsync(InternalEntry, cancellationToken)
.ConfigureAwait(false);
var values = await Finder.GetDatabaseValuesAsync(InternalEntry, cancellationToken).ConfigureAwait(false);

return values == null ? null : new ArrayPropertyValues(InternalEntry, values);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/Internal/IStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public interface IStateManager : IResettableService
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
InternalEntityEntry CreateEntry([NotNull] IDictionary<string, object> values, [NotNull] IEntityType entityType);
InternalEntityEntry CreateEntry([NotNull] IDictionary<string, object?> values, [NotNull] IEntityType entityType);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/ChangeTracking/Internal/RelationshipsSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SetValue(IPropertyBase propertyBase, object? value)

Check.DebugAssert(!IsEmpty, "relationship snapshot is empty");
Check.DebugAssert(
propertyBase is not INavigation navigation || !navigation.IsCollection,
propertyBase is not INavigation { IsCollection : true },
$"property {propertyBase} is is not reference navigation");

_values[propertyBase.GetRelationshipIndex()] = SnapshotValue(propertyBase, value);
Expand Down Expand Up @@ -100,7 +100,7 @@ public void AddRangeToCollection(IPropertyBase propertyBase, IEnumerable<object>

private HashSet<object> GetOrCreateCollection(int index)
{
var snapshot = (HashSet<object>)_values[index]!;
var snapshot = (HashSet<object>?)_values[index];
if (snapshot == null)
{
snapshot = new HashSet<object>(LegacyReferenceEqualityComparer.Instance);
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ public virtual InternalEntityEntry GetOrCreateEntry(object entity, IEntityType?
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual InternalEntityEntry CreateEntry(IDictionary<string, object> values, IEntityType entityType)
public virtual InternalEntityEntry CreateEntry(IDictionary<string, object?> values, IEntityType entityType)
{
var i = 0;
var valuesArray = new object[entityType.PropertyCount()];
var shadowPropertyValuesArray = new object[entityType.ShadowPropertyCount()];
var valuesArray = new object?[entityType.PropertyCount()];
var shadowPropertyValuesArray = new object?[entityType.ShadowPropertyCount()];
foreach (var property in entityType.GetProperties())
{
valuesArray[i++] = values.TryGetValue(property.Name, out var value)
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/ChangeTracking/Internal/ValueGenerationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public virtual void Generate(InternalEntityEntry entry, bool includePrimaryKey =
}
}

private void Log(InternalEntityEntry entry, IProperty property, object generatedValue, bool temporary)
private void Log(InternalEntityEntry entry, IProperty property, object? generatedValue, bool temporary)
{
if (_loggingOptions.IsSensitiveDataLoggingEnabled)
{
Expand Down Expand Up @@ -179,7 +179,7 @@ public virtual bool MayGetTemporaryValue(IProperty property, IEntityType entityT
=> property.RequiresValueGenerator()
&& _valueGeneratorSelector.Select(property, entityType).GeneratesTemporaryValues;

private static void SetGeneratedValue(InternalEntityEntry entry, IProperty property, object generatedValue, bool isTemporary)
private static void SetGeneratedValue(InternalEntityEntry entry, IProperty property, object? generatedValue, bool isTemporary)
{
if (generatedValue != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/PropertyEntry`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public PropertyEntry([NotNull] InternalEntityEntry internalEntry, [NotNull] IPro
/// useful in disconnected scenarios where entities are retrieved with one context instance and
/// saved with a different context instance.
/// </summary>
public new virtual TProperty? OriginalValue
public new virtual TProperty OriginalValue
{
get => InternalEntry.GetOriginalValue<TProperty>(Metadata);
[param: CanBeNull] set => base.OriginalValue = value;
Expand Down
50 changes: 26 additions & 24 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down Expand Up @@ -54,18 +56,18 @@ public class DbContext :
IDbSetCache,
IDbContextPoolable
{
private IDictionary<(Type Type, string Name), object> _sets;
private readonly DbContextOptions _options;

private IDbContextServices _contextServices;
private IDbContextDependencies _dbContextDependencies;
private DatabaseFacade _database;
private ChangeTracker _changeTracker;
private IDictionary<(Type Type, string? Name), object>? _sets;
private IDbContextServices? _contextServices;
private IDbContextDependencies? _dbContextDependencies;
private DatabaseFacade? _database;
private ChangeTracker? _changeTracker;

private IServiceScope _serviceScope;
private IServiceScope? _serviceScope;
private DbContextLease _lease = DbContextLease.InactiveLease;
private DbContextPoolConfigurationSnapshot _configurationSnapshot;
private List<IResettableService> _cachedResettableServices;
private DbContextPoolConfigurationSnapshot? _configurationSnapshot;
private List<IResettableService>? _cachedResettableServices;
private bool _initializing;
private bool _disposed;

Expand Down Expand Up @@ -249,7 +251,7 @@ object IDbSetCache.GetOrAddSet(IDbSetSource source, Type type)
{
CheckDisposed();

_sets ??= new Dictionary<(Type Type, string Name), object>();
_sets ??= new Dictionary<(Type Type, string? Name), object>();

if (!_sets.TryGetValue((type, null), out var set))
{
Expand All @@ -272,7 +274,7 @@ object IDbSetCache.GetOrAddSet(IDbSetSource source, string entityTypeName, Type
{
CheckDisposed();

_sets ??= new Dictionary<(Type Type, string Name), object>();
_sets ??= new Dictionary<(Type Type, string? Name), object>();

if (!_sets.TryGetValue((type, entityTypeName), out var set))
{
Expand Down Expand Up @@ -370,7 +372,7 @@ private IServiceProvider InternalServiceProvider

var scopedServiceProvider = _serviceScope.ServiceProvider;

var contextServices = scopedServiceProvider.GetService<IDbContextServices>();
var contextServices = scopedServiceProvider.GetRequiredService<IDbContextServices>();

contextServices.Initialize(scopedServiceProvider, options, this);

Expand Down Expand Up @@ -669,17 +671,17 @@ await DbContextDependencies.UpdateLogger.OptimisticConcurrencyExceptionAsync(thi
/// <summary>
/// An event fired at the beginning of a call to <see cref="M:SaveChanges" /> or <see cref="M:SaveChangesAsync" />
/// </summary>
public event EventHandler<SavingChangesEventArgs> SavingChanges;
public event EventHandler<SavingChangesEventArgs>? SavingChanges;

/// <summary>
/// An event fired at the end of a call to <see cref="M:SaveChanges" /> or <see cref="M:SaveChangesAsync" />
/// </summary>
public event EventHandler<SavedChangesEventArgs> SavedChanges;
public event EventHandler<SavedChangesEventArgs>? SavedChanges;

/// <summary>
/// An event fired if a call to <see cref="M:SaveChanges" /> or <see cref="M:SaveChangesAsync" /> fails with an exception.
/// </summary>
public event EventHandler<SaveChangesFailedEventArgs> SaveChangesFailed;
public event EventHandler<SaveChangesFailedEventArgs>? SaveChangesFailed;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -722,7 +724,7 @@ void IDbContextPoolable.SetLease(DbContextLease lease)
}
else
{
((IResettableService)_changeTracker)?.ResetState();
((IResettableService?)_changeTracker)?.ResetState();
}

if (_database != null)
Expand Down Expand Up @@ -1684,7 +1686,7 @@ public virtual void RemoveRange([NotNull] IEnumerable<object> entities)
/// <param name="entityType"> The type of entity to find. </param>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
public virtual object Find([NotNull] Type entityType, [CanBeNull] params object[] keyValues)
public virtual object? Find([NotNull] Type entityType, [CanBeNull] params object?[]? keyValues)
{
CheckDisposed();

Expand All @@ -1701,7 +1703,7 @@ public virtual object Find([NotNull] Type entityType, [CanBeNull] params object[
/// <param name="entityType"> The type of entity to find. </param>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
public virtual ValueTask<object> FindAsync([NotNull] Type entityType, [CanBeNull] params object[] keyValues)
public virtual ValueTask<object?> FindAsync([NotNull] Type entityType, [CanBeNull] params object?[]? keyValues)
{
CheckDisposed();

Expand All @@ -1720,9 +1722,9 @@ public virtual ValueTask<object> FindAsync([NotNull] Type entityType, [CanBeNull
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
/// <exception cref="OperationCanceledException"> If the <see cref="CancellationToken"/> is canceled. </exception>
public virtual ValueTask<object> FindAsync(
public virtual ValueTask<object?> FindAsync(
[NotNull] Type entityType,
[CanBeNull] object[] keyValues,
[CanBeNull] object?[]? keyValues,
CancellationToken cancellationToken)
{
CheckDisposed();
Expand All @@ -1740,7 +1742,7 @@ public virtual ValueTask<object> FindAsync(
/// <typeparam name="TEntity"> The type of entity to find. </typeparam>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
public virtual TEntity Find<TEntity>([CanBeNull] params object[] keyValues)
public virtual TEntity? Find<TEntity>([CanBeNull] params object?[]? keyValues)
where TEntity : class
{
CheckDisposed();
Expand All @@ -1758,7 +1760,7 @@ public virtual TEntity Find<TEntity>([CanBeNull] params object[] keyValues)
/// <typeparam name="TEntity"> The type of entity to find. </typeparam>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
public virtual ValueTask<TEntity> FindAsync<TEntity>([CanBeNull] params object[] keyValues)
public virtual ValueTask<TEntity?> FindAsync<TEntity>([CanBeNull] params object?[]? keyValues)
where TEntity : class
{
CheckDisposed();
Expand All @@ -1778,7 +1780,7 @@ public virtual ValueTask<TEntity> FindAsync<TEntity>([CanBeNull] params object[]
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>The entity found, or <see langword="null" />.</returns>
/// <exception cref="OperationCanceledException"> If the <see cref="CancellationToken"/> is canceled. </exception>
public virtual ValueTask<TEntity> FindAsync<TEntity>([CanBeNull] object[] keyValues, CancellationToken cancellationToken)
public virtual ValueTask<TEntity?> FindAsync<TEntity>([CanBeNull] object?[]? keyValues, CancellationToken cancellationToken)
where TEntity : class
{
CheckDisposed();
Expand Down Expand Up @@ -1818,7 +1820,7 @@ public virtual IQueryable<TResult> FromExpression<TResult>([NotNull] Expression<
/// </summary>
/// <returns> A string that represents the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString()
public override string? ToString()
=> base.ToString();

/// <summary>
Expand All @@ -1827,7 +1829,7 @@ public override string ToString()
/// <param name="obj"> The object to compare with the current object. </param>
/// <returns> <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
public override bool Equals(object? obj)
=> base.Equals(obj);

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore/DbContextId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Globalization;

#nullable enable

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down Expand Up @@ -31,7 +33,7 @@ public bool Equals(DbContextId other)
/// </summary>
/// <param name="obj"> The other ID. </param>
/// <returns> <see langword="true" /> if they represent the same leased context; <see langword="false" /> otherwise. </returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
=> obj is DbContextId other && Equals(other);

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore/DbContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down Expand Up @@ -42,7 +44,7 @@ public virtual IEnumerable<IDbContextOptionsExtension> Extensions
/// </summary>
/// <typeparam name="TExtension"> The type of the extension to get. </typeparam>
/// <returns> The extension, or null if none was found. </returns>
public virtual TExtension FindExtension<TExtension>()
public virtual TExtension? FindExtension<TExtension>()
where TExtension : class, IDbContextOptionsExtension
=> _extensions.TryGetValue(typeof(TExtension), out var extension) ? (TExtension)extension : null;

Expand Down
10 changes: 6 additions & 4 deletions src/EFCore/DbContextOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

#nullable enable

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down Expand Up @@ -253,7 +255,7 @@ public virtual DbContextOptionsBuilder LogTo(
{
for (var i = 0; i < categoriesArray.Length; i++)
{
if (eventId.Name.StartsWith(categoriesArray[i], StringComparison.OrdinalIgnoreCase))
if (eventId.Name!.StartsWith(categoriesArray[i], StringComparison.OrdinalIgnoreCase))
{
return true;
}
Expand All @@ -269,7 +271,7 @@ public virtual DbContextOptionsBuilder LogTo(
return LogTo(
action,
(eventId, level) => level >= minimumLevel
&& eventId.Name.StartsWith(singleCategory, StringComparison.OrdinalIgnoreCase),
&& eventId.Name!.StartsWith(singleCategory, StringComparison.OrdinalIgnoreCase),
options);
}

Expand Down Expand Up @@ -663,7 +665,7 @@ private DbContextOptionsBuilder WithOption(Func<CoreOptionsExtension, CoreOption
/// </summary>
/// <returns> A string that represents the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString()
public override string? ToString()
=> base.ToString();

/// <summary>
Expand All @@ -672,7 +674,7 @@ public override string ToString()
/// <param name="obj"> The object to compare with the current object. </param>
/// <returns> <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
public override bool Equals(object? obj)
=> base.Equals(obj);

/// <summary>
Expand Down
Loading