1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System ;
4
5
using System . Collections . Generic ;
5
6
using JetBrains . Annotations ;
6
7
using Microsoft . EntityFrameworkCore . Infrastructure ;
@@ -21,10 +22,24 @@ public static class RelationalEntityTypeExtensions
21
22
/// </summary>
22
23
/// <param name="entityType"> The entity type to get the table name for. </param>
23
24
/// <returns> The name of the table to which the entity type is mapped. </returns>
24
- public static string GetTableName ( [ NotNull ] this IEntityType entityType ) =>
25
- entityType . BaseType != null
26
- ? entityType . GetRootType ( ) . GetTableName ( )
27
- : ( string ) entityType [ RelationalAnnotationNames . TableName ] ?? GetDefaultTableName ( entityType ) ;
25
+ public static string GetTableName ( [ NotNull ] this IEntityType entityType )
26
+ {
27
+ if ( entityType . BaseType != null )
28
+ {
29
+ return entityType . GetRootType ( ) . GetTableName ( ) ;
30
+ }
31
+
32
+ var nameAnnotation = entityType . FindAnnotation ( RelationalAnnotationNames . TableName ) ;
33
+ if ( nameAnnotation != null )
34
+ {
35
+ return ( string ) nameAnnotation . Value ;
36
+ }
37
+
38
+ return ( ( entityType as IConventionEntityType ) ? . GetViewNameConfigurationSource ( ) == null
39
+ && ( ( entityType as IConventionEntityType ) ? . GetDefiningQueryConfigurationSource ( ) == null )
40
+ ? GetDefaultTableName ( entityType )
41
+ : null ) ;
42
+ }
28
43
29
44
/// <summary>
30
45
/// Returns the default table name that would be used for this entity type.
@@ -33,23 +48,23 @@ public static string GetTableName([NotNull] this IEntityType entityType) =>
33
48
/// <returns> The default name of the table to which the entity type would be mapped. </returns>
34
49
public static string GetDefaultTableName ( [ NotNull ] this IEntityType entityType )
35
50
{
36
- if ( entityType . GetDefiningQuery ( ) != null )
37
- {
38
- return null ;
39
- }
40
-
41
51
var ownership = entityType . FindOwnership ( ) ;
42
52
if ( ownership != null
43
53
&& ownership . IsUnique )
44
54
{
45
55
return ownership . PrincipalEntityType . GetTableName ( ) ;
46
56
}
47
57
48
- return Uniquifier . Truncate (
49
- entityType . HasDefiningNavigation ( )
50
- ? $ "{ entityType . DefiningEntityType . GetTableName ( ) } _{ entityType . DefiningNavigationName } "
51
- : entityType . ShortName ( ) ,
52
- entityType . Model . GetMaxIdentifierLength ( ) ) ;
58
+ var name = entityType . ShortName ( ) ;
59
+ if ( entityType . HasDefiningNavigation ( ) )
60
+ {
61
+ var definingTypeName = entityType . DefiningEntityType . GetTableName ( ) ;
62
+ name = definingTypeName != null
63
+ ? $ "{ definingTypeName } _{ entityType . DefiningNavigationName } "
64
+ : $ "{ entityType . DefiningNavigationName } _{ name } ";
65
+ }
66
+
67
+ return Uniquifier . Truncate ( name , entityType . Model . GetMaxIdentifierLength ( ) ) ;
53
68
}
54
69
55
70
/// <summary>
@@ -58,7 +73,7 @@ public static string GetDefaultTableName([NotNull] this IEntityType entityType)
58
73
/// <param name="entityType"> The entity type to set the table name for. </param>
59
74
/// <param name="name"> The name to set. </param>
60
75
public static void SetTableName ( [ NotNull ] this IMutableEntityType entityType , [ CanBeNull ] string name )
61
- => entityType . SetOrRemoveAnnotation (
76
+ => entityType . SetAnnotation (
62
77
RelationalAnnotationNames . TableName ,
63
78
Check . NullButNotEmpty ( name , nameof ( name ) ) ) ;
64
79
@@ -70,7 +85,7 @@ public static void SetTableName([NotNull] this IMutableEntityType entityType, [C
70
85
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
71
86
public static void SetTableName (
72
87
[ NotNull ] this IConventionEntityType entityType , [ CanBeNull ] string name , bool fromDataAnnotation = false )
73
- => entityType . SetOrRemoveAnnotation (
88
+ => entityType . SetAnnotation (
74
89
RelationalAnnotationNames . TableName ,
75
90
Check . NullButNotEmpty ( name , nameof ( name ) ) ,
76
91
fromDataAnnotation ) ;
@@ -97,7 +112,7 @@ public static string GetSchema([NotNull] this IEntityType entityType) =>
97
112
/// <summary>
98
113
/// Returns the default database schema that would be used for this entity type.
99
114
/// </summary>
100
- /// <param name="entityType"> The entity type to get the table name for. </param>
115
+ /// <param name="entityType"> The entity type to get the table schema for. </param>
101
116
/// <returns> The default database schema to which the entity type would be mapped. </returns>
102
117
public static string GetDefaultSchema ( [ NotNull ] this IEntityType entityType )
103
118
{
@@ -109,7 +124,7 @@ public static string GetDefaultSchema([NotNull] this IEntityType entityType)
109
124
}
110
125
111
126
return entityType . HasDefiningNavigation ( )
112
- ? entityType . DefiningEntityType . GetSchema ( )
127
+ ? entityType . DefiningEntityType . GetSchema ( ) ?? entityType . Model . GetDefaultSchema ( )
113
128
: entityType . Model . GetDefaultSchema ( ) ;
114
129
}
115
130
@@ -174,28 +189,119 @@ public static IEnumerable<IViewMapping> GetViewMappings([NotNull] this IEntityTy
174
189
/// </summary>
175
190
/// <param name="entityType"> The entity type to get the view name for. </param>
176
191
/// <returns> The name of the view to which the entity type is mapped. </returns>
177
- public static string GetViewName ( [ NotNull ] this IEntityType entityType )
192
+ public static string GetViewName ( [ NotNull ] this IEntityType entityType ) =>
193
+ entityType . BaseType != null
194
+ ? entityType . GetRootType ( ) . GetViewName ( )
195
+ : ( string ) entityType [ RelationalAnnotationNames . ViewName ]
196
+ ?? ( ( ( entityType as IConventionEntityType ) ? . GetDefiningQueryConfigurationSource ( ) == null )
197
+ ? GetDefaultViewName ( entityType )
198
+ : null ) ;
199
+
200
+ /// <summary>
201
+ /// Returns the default view name that would be used for this entity type.
202
+ /// </summary>
203
+ /// <param name="entityType"> The entity type to get the table name for. </param>
204
+ /// <returns> The default name of the table to which the entity type would be mapped. </returns>
205
+ public static string GetDefaultViewName ( [ NotNull ] this IEntityType entityType )
178
206
{
179
- if ( entityType . BaseType != null )
180
- {
181
- return entityType . GetRootType ( ) . GetViewName ( ) ;
182
- }
207
+ var ownership = entityType . FindOwnership ( ) ;
208
+ return ownership != null
209
+ && ownership . IsUnique
210
+ ? ownership . PrincipalEntityType . GetViewName ( )
211
+ : null ;
212
+ }
183
213
184
- if ( entityType . FindAnnotation ( RelationalAnnotationNames . ViewDefinition ) != null )
185
- {
186
- return entityType . GetTableName ( ) ;
187
- }
214
+ /// <summary>
215
+ /// Sets the name of the view to which the entity type is mapped.
216
+ /// </summary>
217
+ /// <param name="entityType"> The entity type to set the view name for. </param>
218
+ /// <param name="name"> The name to set. </param>
219
+ public static void SetViewName ( [ NotNull ] this IMutableEntityType entityType , [ CanBeNull ] string name )
220
+ => entityType . SetAnnotation (
221
+ RelationalAnnotationNames . ViewName ,
222
+ Check . NullButNotEmpty ( name , nameof ( name ) ) ) ;
223
+
224
+ /// <summary>
225
+ /// Sets the name of the view to which the entity type is mapped.
226
+ /// </summary>
227
+ /// <param name="entityType"> The entity type to set the view name for. </param>
228
+ /// <param name="name"> The name to set. </param>
229
+ /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
230
+ public static void SetViewName (
231
+ [ NotNull ] this IConventionEntityType entityType , [ CanBeNull ] string name , bool fromDataAnnotation = false )
232
+ => entityType . SetAnnotation (
233
+ RelationalAnnotationNames . ViewName ,
234
+ Check . NullButNotEmpty ( name , nameof ( name ) ) ,
235
+ fromDataAnnotation ) ;
236
+
237
+ /// <summary>
238
+ /// Gets the <see cref="ConfigurationSource" /> for the view name.
239
+ /// </summary>
240
+ /// <param name="entityType"> The entity type to find configuration source for. </param>
241
+ /// <returns> The <see cref="ConfigurationSource" /> for the view name. </returns>
242
+ public static ConfigurationSource ? GetViewNameConfigurationSource ( [ NotNull ] this IConventionEntityType entityType )
243
+ => entityType . FindAnnotation ( RelationalAnnotationNames . ViewName )
244
+ ? . GetConfigurationSource ( ) ;
245
+
246
+ /// <summary>
247
+ /// Returns the database schema that contains the mapped view.
248
+ /// </summary>
249
+ /// <param name="entityType"> The entity type to get the view schema for. </param>
250
+ /// <returns> The database schema that contains the mapped view. </returns>
251
+ public static string GetViewSchema ( [ NotNull ] this IEntityType entityType ) =>
252
+ entityType . BaseType != null
253
+ ? entityType . GetRootType ( ) . GetViewSchema ( )
254
+ : ( string ) entityType [ RelationalAnnotationNames . ViewSchema ] ?? GetDefaultViewSchema ( entityType ) ;
188
255
256
+ /// <summary>
257
+ /// Returns the default database schema that would be used for this entity view.
258
+ /// </summary>
259
+ /// <param name="entityType"> The entity type to get the view schema for. </param>
260
+ /// <returns> The default database schema to which the entity type would be mapped. </returns>
261
+ public static string GetDefaultViewSchema ( [ NotNull ] this IEntityType entityType )
262
+ {
189
263
var ownership = entityType . FindOwnership ( ) ;
190
264
if ( ownership != null
191
265
&& ownership . IsUnique )
192
266
{
193
- return ownership . PrincipalEntityType . GetViewName ( ) ;
267
+ return ownership . PrincipalEntityType . GetViewSchema ( ) ;
194
268
}
195
269
196
270
return null ;
197
271
}
198
272
273
+ /// <summary>
274
+ /// Sets the database schema that contains the mapped view.
275
+ /// </summary>
276
+ /// <param name="entityType"> The entity type to set the view schema for. </param>
277
+ /// <param name="value"> The value to set. </param>
278
+ public static void SetViewSchema ( [ NotNull ] this IMutableEntityType entityType , [ CanBeNull ] string value )
279
+ => entityType . SetOrRemoveAnnotation (
280
+ RelationalAnnotationNames . ViewSchema ,
281
+ Check . NullButNotEmpty ( value , nameof ( value ) ) ) ;
282
+
283
+ /// <summary>
284
+ /// Sets the database schema that contains the mapped view.
285
+ /// </summary>
286
+ /// <param name="entityType"> The entity type to set the view schema for. </param>
287
+ /// <param name="value"> The value to set. </param>
288
+ /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
289
+ public static void SetViewSchema (
290
+ [ NotNull ] this IConventionEntityType entityType , [ CanBeNull ] string value , bool fromDataAnnotation = false )
291
+ => entityType . SetOrRemoveAnnotation (
292
+ RelationalAnnotationNames . ViewSchema ,
293
+ Check . NullButNotEmpty ( value , nameof ( value ) ) ,
294
+ fromDataAnnotation ) ;
295
+
296
+ /// <summary>
297
+ /// Gets the <see cref="ConfigurationSource" /> for the view schema.
298
+ /// </summary>
299
+ /// <param name="entityType"> The entity type to find configuration source for. </param>
300
+ /// <returns> The <see cref="ConfigurationSource" /> for the view schema. </returns>
301
+ public static ConfigurationSource ? GetViewSchemaConfigurationSource ( [ NotNull ] this IConventionEntityType entityType )
302
+ => entityType . FindAnnotation ( RelationalAnnotationNames . ViewSchema )
303
+ ? . GetConfigurationSource ( ) ;
304
+
199
305
/// <summary>
200
306
/// Finds an <see cref="ICheckConstraint" /> with the given name.
201
307
/// </summary>
0 commit comments