-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Basic support for non-nullable references
- Loading branch information
Showing
7 changed files
with
268 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
src/EFCore/Metadata/Conventions/Internal/RequiredNavigationAttributeConvention.cs
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
src/EFCore/Metadata/Conventions/Internal/RequiredNavigationConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class RequiredNavigationConvention : INavigationAddedConvention | ||
{ | ||
private const string NullableAttributeFullName = "System.Runtime.CompilerServices.NullableAttribute"; | ||
private Type _nullableAttrType; | ||
private FieldInfo _nullableFlagsFieldInfo; | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public RequiredNavigationConvention([NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger) | ||
{ | ||
Logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
protected virtual IDiagnosticsLogger<DbLoggerCategory.Model> Logger { get; } | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder, Navigation navigation) | ||
{ | ||
Check.NotNull(relationshipBuilder, nameof(relationshipBuilder)); | ||
Check.NotNull(navigation, nameof(navigation)); | ||
|
||
var entityType = navigation.DeclaringEntityType; | ||
if (!entityType.HasClrType()) | ||
{ | ||
return relationshipBuilder; | ||
} | ||
|
||
var property = entityType.GetRuntimeProperties().Find(navigation.Name); | ||
if (property == null || !IsRequired(property, out var withRequiredAttr)) | ||
{ | ||
return relationshipBuilder; | ||
} | ||
|
||
if (!navigation.IsDependentToPrincipal()) | ||
{ | ||
var inverse = navigation.FindInverse(); | ||
if (inverse != null) | ||
{ | ||
if (IsRequired(inverse.PropertyInfo, out _)) | ||
{ | ||
Logger.RequiredAttributeOnBothNavigations(navigation, inverse); | ||
return relationshipBuilder; | ||
} | ||
} | ||
|
||
if (!navigation.ForeignKey.IsUnique | ||
|| relationshipBuilder.Metadata.GetPrincipalEndConfigurationSource() != null) | ||
{ | ||
return relationshipBuilder; | ||
} | ||
|
||
var newRelationshipBuilder = relationshipBuilder.RelatedEntityTypes( | ||
relationshipBuilder.Metadata.DeclaringEntityType, | ||
relationshipBuilder.Metadata.PrincipalEntityType, | ||
ConfigurationSource.Convention); | ||
|
||
if (newRelationshipBuilder == null) | ||
{ | ||
return relationshipBuilder; | ||
} | ||
|
||
Logger.RequiredAttributeOnDependent(newRelationshipBuilder.Metadata.DependentToPrincipal); | ||
relationshipBuilder = newRelationshipBuilder; | ||
} | ||
|
||
return relationshipBuilder.IsRequired(true, withRequiredAttr ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention) | ||
?? relationshipBuilder; | ||
} | ||
|
||
bool IsRequired(PropertyInfo property, out bool withRequiredAttr) | ||
{ | ||
if (Attribute.IsDefined(property, typeof(RequiredAttribute))) | ||
{ | ||
withRequiredAttr = true; | ||
return true; | ||
} | ||
|
||
if (Attribute.GetCustomAttributes(property) is Attribute[] attributes && | ||
attributes.FirstOrDefault(a => a.GetType().FullName == NullableAttributeFullName) is Attribute attribute) | ||
{ | ||
// In theory there may be multiple versions of [Nullable] in the same model. Cache the attribute's FieldInfo. | ||
if (attribute.GetType() != _nullableAttrType) | ||
{ | ||
_nullableFlagsFieldInfo = attribute.GetType().GetField("NullableFlags"); | ||
_nullableAttrType = attribute.GetType(); | ||
} | ||
|
||
// For the interpretation of NullableFlags, see | ||
// https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-reference-types.md#annotations | ||
if (_nullableFlagsFieldInfo?.GetValue(attribute) is byte[] flags && flags.Length >= 0 && flags[0] == 1) | ||
{ | ||
withRequiredAttr = false; | ||
return true; | ||
} | ||
} | ||
|
||
withRequiredAttr = false; | ||
return false; | ||
} | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
src/EFCore/Metadata/Conventions/Internal/RequiredPropertyAttributeConvention.cs
This file was deleted.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
src/EFCore/Metadata/Conventions/Internal/RequiredPropertyConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class RequiredPropertyConvention : IPropertyAddedConvention, IPropertyFieldChangedConvention | ||
{ | ||
private const string NullableAttributeFullName = "System.Runtime.CompilerServices.NullableAttribute"; | ||
private Type _nullableAttrType; | ||
private FieldInfo _nullableFlagsFieldInfo; | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
protected virtual IDiagnosticsLogger<DbLoggerCategory.Model> Logger { get; } | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public RequiredPropertyConvention([NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger) | ||
{ | ||
Logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder) | ||
{ | ||
Check.NotNull(propertyBuilder, nameof(propertyBuilder)); | ||
|
||
var memberInfo = propertyBuilder.Metadata.GetIdentifyingMemberInfo(); | ||
if (memberInfo == null || !IsRequired(memberInfo, out var withRequiredAttr)) | ||
{ | ||
return propertyBuilder; | ||
} | ||
|
||
propertyBuilder.IsRequired(true, withRequiredAttr ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); | ||
|
||
return propertyBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual bool Apply(InternalPropertyBuilder propertyBuilder, FieldInfo oldFieldInfo) | ||
{ | ||
Apply(propertyBuilder); | ||
return true; | ||
} | ||
|
||
bool IsRequired(MemberInfo member, out bool withRequiredAttr) | ||
{ | ||
if (Attribute.IsDefined(member, typeof(RequiredAttribute))) | ||
{ | ||
withRequiredAttr = true; | ||
return true; | ||
} | ||
|
||
if (Attribute.GetCustomAttributes(member) is Attribute[] attributes && | ||
attributes.FirstOrDefault(a => a.GetType().FullName == NullableAttributeFullName) is Attribute attribute) | ||
{ | ||
// In theory there may be multiple versions of [Nullable] in the same model. Cache the attribute's FieldInfo. | ||
if (attribute.GetType() != _nullableAttrType) | ||
{ | ||
_nullableFlagsFieldInfo = attribute.GetType().GetField("NullableFlags"); | ||
_nullableAttrType = attribute.GetType(); | ||
} | ||
|
||
// For the interpretation of NullableFlags, see | ||
// https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-reference-types.md#annotations | ||
if (_nullableFlagsFieldInfo?.GetValue(attribute) is byte[] flags && flags.Length >= 0 && flags[0] == 1) | ||
{ | ||
withRequiredAttr = false; | ||
return true; | ||
} | ||
} | ||
|
||
withRequiredAttr = false; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.