-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathValueGenerationConvention.cs
237 lines (219 loc) · 10.4 KB
/
ValueGenerationConvention.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// 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.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
#nullable enable
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// A convention that configures store value generation as <see cref="ValueGenerated.OnAdd" /> on properties that are
/// part of the primary key and not part of any foreign keys.
/// </summary>
public class ValueGenerationConvention :
IEntityTypePrimaryKeyChangedConvention,
IForeignKeyAddedConvention,
IForeignKeyRemovedConvention,
IForeignKeyPropertiesChangedConvention,
IEntityTypeBaseTypeChangedConvention,
IForeignKeyOwnershipChangedConvention
{
/// <summary>
/// Creates a new instance of <see cref="ValueGenerationConvention" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param>
public ValueGenerationConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
{
Dependencies = dependencies;
}
/// <summary>
/// Parameter object containing service dependencies.
/// </summary>
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; }
/// <summary>
/// Called after a foreign key is added to the entity type.
/// </summary>
/// <param name="relationshipBuilder"> The builder for the foreign key. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessForeignKeyAdded(
IConventionForeignKeyBuilder relationshipBuilder,
IConventionContext<IConventionForeignKeyBuilder> context)
{
var foreignKey = relationshipBuilder.Metadata;
if (!foreignKey.IsBaseLinking())
{
foreach (var property in foreignKey.Properties)
{
property.Builder.ValueGenerated(ValueGenerated.Never);
}
}
}
/// <summary>
/// Called after a foreign key is removed.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type. </param>
/// <param name="foreignKey"> The removed foreign key. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessForeignKeyRemoved(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionForeignKey foreignKey,
IConventionContext<IConventionForeignKey> context)
{
OnForeignKeyRemoved(foreignKey.Properties);
}
/// <summary>
/// Called after the foreign key properties or principal key are changed.
/// </summary>
/// <param name="relationshipBuilder"> The builder for the foreign key. </param>
/// <param name="oldDependentProperties"> The old foreign key properties. </param>
/// <param name="oldPrincipalKey"> The old principal key. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessForeignKeyPropertiesChanged(
IConventionForeignKeyBuilder relationshipBuilder,
IReadOnlyList<IConventionProperty> oldDependentProperties,
IConventionKey oldPrincipalKey,
IConventionContext<IReadOnlyList<IConventionProperty>> context)
{
var foreignKey = relationshipBuilder.Metadata;
if (!foreignKey.Properties.SequenceEqual(oldDependentProperties))
{
OnForeignKeyRemoved(oldDependentProperties);
if (relationshipBuilder.Metadata.IsInModel
&& !foreignKey.IsBaseLinking())
{
foreach (var property in foreignKey.Properties)
{
property.Builder.ValueGenerated(ValueGenerated.Never);
}
}
}
}
private void OnForeignKeyRemoved(IReadOnlyList<IConventionProperty> foreignKeyProperties)
{
foreach (var property in foreignKeyProperties)
{
var pk = property.FindContainingPrimaryKey();
if (pk == null)
{
if (property.IsInModel)
{
property.Builder.ValueGenerated(GetValueGenerated(property));
}
}
else
{
foreach (var keyProperty in pk.Properties)
{
keyProperty.Builder.ValueGenerated(GetValueGenerated(property));
}
}
}
}
/// <summary>
/// Called after the primary key for an entity type is changed.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type. </param>
/// <param name="newPrimaryKey"> The new primary key. </param>
/// <param name="previousPrimaryKey"> The old primary key. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessEntityTypePrimaryKeyChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionKey? newPrimaryKey,
IConventionKey? previousPrimaryKey,
IConventionContext<IConventionKey> context)
{
if (previousPrimaryKey != null)
{
foreach (var property in previousPrimaryKey.Properties)
{
if (property.IsInModel)
{
property.Builder.ValueGenerated(ValueGenerated.Never);
}
}
}
if (newPrimaryKey?.IsInModel == true)
{
foreach (var property in newPrimaryKey.Properties)
{
property.Builder.ValueGenerated(GetValueGenerated(property));
}
}
}
/// <summary>
/// Called after the base type of an entity type changes.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type. </param>
/// <param name="newBaseType"> The new base entity type. </param>
/// <param name="oldBaseType"> The old base entity type. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessEntityTypeBaseTypeChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionEntityType? newBaseType,
IConventionEntityType? oldBaseType,
IConventionContext<IConventionEntityType> context)
{
if (entityTypeBuilder.Metadata.BaseType != newBaseType)
{
return;
}
foreach (var property in entityTypeBuilder.Metadata.GetProperties())
{
property.Builder.ValueGenerated(GetValueGenerated(property));
}
}
/// <summary>
/// Returns the store value generation strategy to set for the given property.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The store value generation strategy to set for the given property. </returns>
protected virtual ValueGenerated? GetValueGenerated([NotNull] IConventionProperty property)
=> GetValueGenerated((IProperty)property);
/// <summary>
/// Returns the store value generation strategy to set for the given property.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The store value generation strategy to set for the given property. </returns>
public static ValueGenerated? GetValueGenerated([NotNull] IProperty property)
=> !property.GetContainingForeignKeys().Any(fk => !fk.IsBaseLinking())
&& ShouldHaveGeneratedProperty(property.FindContainingPrimaryKey())
&& CanBeGenerated(property)
? ValueGenerated.OnAdd
: (ValueGenerated?)null;
private static bool ShouldHaveGeneratedProperty(IKey? key)
=> key != null
&& key.DeclaringEntityType.IsOwned() is var onOwnedType
&& (onOwnedType && key.Properties.Count(p => !p.IsForeignKey()) == 1
|| !onOwnedType && key.Properties.Count == 1);
/// <summary>
/// Indicates whether the specified property can have the value generated by the store or by a non-temporary value generator
/// when not set.
/// </summary>
/// <param name="property"> The key property that might be store generated. </param>
/// <returns> A value indicating whether the specified property should have the value generated by the store. </returns>
private static bool CanBeGenerated(IProperty property)
{
var propertyType = property.ClrType.UnwrapNullableType();
return (propertyType.IsInteger()
&& propertyType != typeof(byte))
|| propertyType == typeof(Guid);
}
/// <summary>
/// Called after the ownership value for a foreign key is changed.
/// </summary>
/// <param name="relationshipBuilder"> The builder for the foreign key. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessForeignKeyOwnershipChanged(
IConventionForeignKeyBuilder relationshipBuilder,
IConventionContext<bool?> context)
{
foreach (var property in relationshipBuilder.Metadata.DeclaringEntityType.GetProperties())
{
property.Builder.ValueGenerated(GetValueGenerated(property));
}
}
}
}