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

Annotate metadata for nullability, leaving only builders and conventions #23381

Merged
11 commits merged into from
Nov 19, 2020
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal
{
/// <summary>
roji marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -27,11 +29,11 @@ public static bool IsOrdinalKeyProperty([NotNull] this IProperty property)
property.DeclaringEntityType.IsOwned(), $"Expected {property.DeclaringEntityType.DisplayName()} to be owned.");
Check.DebugAssert(property.GetJsonPropertyName().Length == 0, $"Expected {property.Name} to be non-persisted.");

return property.IsPrimaryKey()
return property.FindContainingPrimaryKey() is IKey key
&& key.Properties.Count > 1
&& !property.IsForeignKey()
&& property.ClrType == typeof(int)
&& property.ValueGenerated == ValueGenerated.OnAdd
&& property.DeclaringEntityType.FindPrimaryKey().Properties.Count > 1;
&& property.ValueGenerated == ValueGenerated.OnAdd;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)

var property = member.MemberInfo != null
? entityType.FindProperty(member.MemberInfo)
: entityType.FindProperty(member.Name);
: entityType.FindProperty(member.Name!);

if (property != null)
{
Expand Down Expand Up @@ -1424,7 +1424,10 @@ private Expression CreatePropertyAccessExpression(Expression target, IProperty p
{
case ConstantExpression constantExpression:
return Expression.Constant(
property.GetGetter().GetClrValue(constantExpression.Value), property.ClrType.MakeNullable());
constantExpression.Value is null
? null
: property.GetGetter().GetClrValue(constantExpression.Value),
property.ClrType.MakeNullable());

case MethodCallExpression methodCallExpression
when methodCallExpression.Method.IsGenericMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ protected override Expression VisitExtension(Expression extensionExpression)

var navigation = member.MemberInfo != null
? entityType.FindNavigation(member.MemberInfo)
: entityType.FindNavigation(member.Name);
: entityType.FindNavigation(member.Name!);

if (navigation == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private static LambdaExpression GenerateFixup(
Type entityType,
Type relatedEntityType,
INavigationBase navigation,
INavigationBase inverseNavigation)
INavigationBase? inverseNavigation)
{
var entityParameter = Expression.Parameter(entityType);
var relatedEntityParameter = Expression.Parameter(relatedEntityType);
Expand Down
92 changes: 47 additions & 45 deletions src/EFCore.Relational/Extensions/RelationalModelExtensions.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
/// <summary>
Expand All @@ -30,7 +32,7 @@ public static string FormatColumns(
/// <param name="properties"> The properties to format. </param>
/// <param name="storeObject"> The identifier of the table-like store object containing the column. </param>
/// <returns> A list of column names. </returns>
public static IReadOnlyList<string> GetColumnNames(
public static IReadOnlyList<string>? GetColumnNames(
[NotNull] this IEnumerable<IProperty> properties,
in StoreObjectIdentifier storeObject)
{
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/CheckConstraintExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/ColumnExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/ColumnMappingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/DbFunctionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/FunctionColumnExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/FunctionMappingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/ICheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/EFCore.Relational/Metadata/IColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.Linq;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down Expand Up @@ -65,7 +67,7 @@ bool IsRowVersion
/// <summary>
/// Returns the object that is used as the default value for this column.
/// </summary>
public virtual object DefaultValue
public virtual object? DefaultValue
{
get
{
Expand All @@ -82,14 +84,14 @@ public virtual object DefaultValue
/// <summary>
/// Returns the SQL expression that is used as the default value for this column.
/// </summary>
public virtual string DefaultValueSql
public virtual string? DefaultValueSql
=> PropertyMappings.First().Property
.GetDefaultValueSql(StoreObjectIdentifier.Table(Table.Name, Table.Schema));

/// <summary>
/// Returns the SQL expression that is used as the computed value for this column.
/// </summary>
public virtual string ComputedColumnSql
public virtual string? ComputedColumnSql
=> PropertyMappings.First().Property
.GetComputedColumnSql(StoreObjectIdentifier.Table(Table.Name, Table.Schema));

Expand All @@ -104,14 +106,14 @@ public virtual bool? IsStored
/// <summary>
/// Comment for this column
/// </summary>
public virtual string Comment
public virtual string? Comment
=> PropertyMappings.First().Property
.GetComment(StoreObjectIdentifier.Table(Table.Name, Table.Schema));

/// <summary>
/// Collation for this column
/// </summary>
public virtual string Collation
public virtual string? Collation
=> PropertyMappings.First().Property
.GetCollation(StoreObjectIdentifier.Table(Table.Name, Table.Schema));
}
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/IColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Infrastructure;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/IColumnMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Metadata/IColumnMappingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand Down
16 changes: 9 additions & 7 deletions src/EFCore.Relational/Metadata/IConventionDbFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand All @@ -24,7 +26,7 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// <summary>
/// Gets the builder that can be used to configure this function.
/// </summary>
new IConventionDbFunctionBuilder Builder { get; }
new IConventionDbFunctionBuilder? Builder { get; }

/// <summary>
/// Gets the configuration source for this function.
Expand All @@ -38,7 +40,7 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// <param name="name"> The name of the function in the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
string SetName([CanBeNull] string name, bool fromDataAnnotation = false);
string? SetName([CanBeNull] string? name, bool fromDataAnnotation = false);

/// <summary>
/// Gets the configuration source for <see cref="IDbFunction.Name" />.
Expand All @@ -52,7 +54,7 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// <param name="schema"> The schema of the function in the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
string SetSchema([CanBeNull] string schema, bool fromDataAnnotation = false);
string? SetSchema([CanBeNull] string? schema, bool fromDataAnnotation = false);

/// <summary>
/// Gets the configuration source for <see cref="IDbFunction.Schema" />.
Expand Down Expand Up @@ -94,7 +96,7 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// <param name="storeType"> The store type of the function in the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
string SetStoreType([CanBeNull] string storeType, bool fromDataAnnotation = false);
string? SetStoreType([CanBeNull] string? storeType, bool fromDataAnnotation = false);

/// <summary>
/// Gets the configuration source for <see cref="IDbFunction.StoreType" />.
Expand All @@ -108,7 +110,7 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// <param name="typeMapping"> The type mapping of the function in the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
RelationalTypeMapping SetTypeMapping([CanBeNull] RelationalTypeMapping typeMapping, bool fromDataAnnotation = false);
RelationalTypeMapping? SetTypeMapping([CanBeNull] RelationalTypeMapping? typeMapping, bool fromDataAnnotation = false);

/// <summary>
/// Gets the configuration source for <see cref="IDbFunction.TypeMapping" />.
Expand All @@ -124,8 +126,8 @@ public interface IConventionDbFunction : IConventionAnnotatable, IDbFunction
/// </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured value. </returns>
Func<IReadOnlyCollection<SqlExpression>, SqlExpression> SetTranslation(
[CanBeNull] Func<IReadOnlyCollection<SqlExpression>, SqlExpression> translation,
Func<IReadOnlyCollection<SqlExpression>, SqlExpression>? SetTranslation(
[CanBeNull] Func<IReadOnlyCollection<SqlExpression>, SqlExpression>? translation,
bool fromDataAnnotation = false);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
Expand All @@ -20,7 +22,7 @@ public interface IConventionDbFunctionParameter : IConventionAnnotatable, IDbFun
/// <summary>
/// The <see cref="IConventionDbFunctionParameterBuilder" /> for configuring this function parameter.
/// </summary>
new IConventionDbFunctionParameterBuilder Builder { get; }
new IConventionDbFunctionParameterBuilder? Builder { get; }

/// <summary>
/// Returns the configuration source for the parameter.
Expand All @@ -33,7 +35,7 @@ public interface IConventionDbFunctionParameter : IConventionAnnotatable, IDbFun
/// </summary>
/// <param name="storeType"> The store type of the parameter. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
string SetStoreType([CanBeNull] string storeType, bool fromDataAnnotation = false);
string? SetStoreType([CanBeNull] string? storeType, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunctionParameter.StoreType" />.
Expand All @@ -46,7 +48,7 @@ public interface IConventionDbFunctionParameter : IConventionAnnotatable, IDbFun
/// </summary>
/// <param name="typeMapping"> The type mapping of the parameter in the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
RelationalTypeMapping SetTypeMapping([CanBeNull] RelationalTypeMapping typeMapping, bool fromDataAnnotation = false);
RelationalTypeMapping? SetTypeMapping([CanBeNull] RelationalTypeMapping? typeMapping, bool fromDataAnnotation = false);

/// <summary>
/// Returns the configuration source for <see cref="IDbFunctionParameter.TypeMapping" />.
Expand Down
Loading