forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonNullableReferencePropertyConvention.cs
97 lines (87 loc) · 3.63 KB
/
NonNullableReferencePropertyConvention.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
/// <summary>
/// A convention that configures the properties of non-nullable types as required.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples.
/// </remarks>
public class NonNullableReferencePropertyConvention : NonNullableConventionBase,
IPropertyAddedConvention,
IPropertyFieldChangedConvention,
IComplexPropertyAddedConvention,
IComplexPropertyFieldChangedConvention
{
/// <summary>
/// Creates a new instance of <see cref="NonNullableReferencePropertyConvention" />.
/// </summary>
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param>
public NonNullableReferencePropertyConvention(ProviderConventionSetBuilderDependencies dependencies)
: base(dependencies)
{
}
private void Process(IConventionPropertyBuilder propertyBuilder)
{
if (propertyBuilder.Metadata.GetIdentifyingMemberInfo() is MemberInfo memberInfo
&& TryGetNullabilityInfo(propertyBuilder.ModelBuilder, memberInfo, out var nullabilityInfo))
{
if (nullabilityInfo.ReadState == NullabilityState.NotNull)
{
propertyBuilder.IsRequired(true);
}
// If there's an element type, this is a primitive collection; check and apply the element's nullability as well.
if (propertyBuilder.Metadata.GetElementType() is IConventionElementType elementType
&& nullabilityInfo is
{ ElementType.ReadState: NullabilityState.NotNull } or
{ GenericTypeArguments: [{ ReadState: NullabilityState.NotNull }] })
{
elementType.SetIsNullable(false);
}
}
}
private void Process(IConventionComplexPropertyBuilder propertyBuilder)
{
if (propertyBuilder.Metadata.GetIdentifyingMemberInfo() is MemberInfo memberInfo
&& TryGetNullabilityInfo(propertyBuilder.ModelBuilder, memberInfo, out var nullabilityInfo)
&& nullabilityInfo.ReadState == NullabilityState.NotNull)
{
propertyBuilder.IsRequired(true);
}
}
/// <inheritdoc />
public virtual void ProcessPropertyAdded(
IConventionPropertyBuilder propertyBuilder,
IConventionContext<IConventionPropertyBuilder> context)
=> Process(propertyBuilder);
/// <inheritdoc />
public virtual void ProcessPropertyFieldChanged(
IConventionPropertyBuilder propertyBuilder,
FieldInfo? newFieldInfo,
FieldInfo? oldFieldInfo,
IConventionContext<FieldInfo> context)
{
if (propertyBuilder.Metadata.PropertyInfo == null)
{
Process(propertyBuilder);
}
}
/// <inheritdoc />
public void ProcessComplexPropertyAdded(
IConventionComplexPropertyBuilder propertyBuilder,
IConventionContext<IConventionComplexPropertyBuilder> context)
=> Process(propertyBuilder);
/// <inheritdoc />
public void ProcessComplexPropertyFieldChanged(
IConventionComplexPropertyBuilder propertyBuilder,
FieldInfo? newFieldInfo,
FieldInfo? oldFieldInfo,
IConventionContext<FieldInfo> context)
{
if (propertyBuilder.Metadata.PropertyInfo == null)
{
Process(propertyBuilder);
}
}
}