11using System ;
2- using System . Collections ;
32using System . Collections . Concurrent ;
43using System . Collections . Generic ;
54using System . Data ;
@@ -31,7 +30,7 @@ namespace LinqToDB.EntityFrameworkCore
3130 /// <summary>
3231 /// LINQ To DB metadata reader for EF.Core model.
3332 /// </summary>
34- internal class EFCoreMetadataReader : IMetadataReader
33+ internal sealed class EFCoreMetadataReader : IMetadataReader
3534 {
3635 readonly IModel ? _model ;
3736 private readonly RelationalSqlTranslatingExpressionVisitorDependencies ? _dependencies ;
@@ -40,8 +39,7 @@ internal class EFCoreMetadataReader : IMetadataReader
4039 private readonly ConcurrentDictionary < MemberInfo , EFCoreExpressionAttribute ? > _calculatedExtensions = new ( ) ;
4140 private readonly IDiagnosticsLogger < DbLoggerCategory . Query > ? _logger ;
4241
43- public EFCoreMetadataReader (
44- IModel ? model , IInfrastructure < IServiceProvider > ? accessor )
42+ public EFCoreMetadataReader ( IModel ? model , IInfrastructure < IServiceProvider > ? accessor )
4543 {
4644 _model = model ;
4745 if ( accessor != null )
@@ -61,7 +59,7 @@ public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute
6159 if ( typeof ( T ) == typeof ( TableAttribute ) )
6260 {
6361 var storeObjectId = GetStoreObjectIdentifier ( et ) ;
64- return new [ ] { ( T ) ( Attribute ) new TableAttribute ( storeObjectId ! . Value . Name ) { Schema = storeObjectId ! . Value . Schema } } ;
62+ return new [ ] { ( T ) ( Attribute ) new TableAttribute ( ) { Schema = storeObjectId ? . Schema , Name = storeObjectId ? . Name } } ;
6563 }
6664 if ( typeof ( T ) == typeof ( QueryFilterAttribute ) )
6765 {
@@ -208,7 +206,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
208206
209207 if ( prop != null )
210208 {
211- var discriminator = et . GetDiscriminatorProperty ( ) ;
209+ var discriminator = et . FindDiscriminatorProperty ( ) ;
212210
213211 var isPrimaryKey = prop . IsPrimaryKey ( ) ;
214212 var primaryKeyOrder = 0 ;
@@ -225,14 +223,14 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
225223 if ( _annotationProvider != null && storeObjectId != null )
226224 {
227225 if ( prop . FindColumn ( storeObjectId . Value ) is IColumn column )
228- annotations = annotations . Concat ( _annotationProvider . For ( column ) ) ;
226+ annotations = annotations . Concat ( _annotationProvider . For ( column , false ) ) ;
229227 }
230228
231229 var isIdentity = annotations
232230 . Any ( a =>
233231 {
234232 if ( a . Name . EndsWith ( ":ValueGenerationStrategy" ) )
235- return a . Value ? . ToString ( ) ! . Contains ( "Identity" ) == true ;
233+ return a . Value ? . ToString ( ) ? . Contains ( "Identity" ) == true ;
236234
237235 if ( a . Name . EndsWith ( ":Autoincrement" ) )
238236 return a . Value is bool b && b ;
@@ -242,7 +240,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
242240 {
243241 if ( a . Value is string str )
244242 {
245- return str . ToLower ( ) . Contains ( "nextval" ) ;
243+ return str . ToLowerInvariant ( ) . Contains ( "nextval" ) ;
246244 }
247245 }
248246
@@ -259,7 +257,8 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
259257 }
260258 else
261259 {
262- dataType = SqlDataType . GetDataType ( typeMapping . ClrType ) . Type . DataType ;
260+ var ms = _model != null ? LinqToDBForEFTools . GetMappingSchema ( _model , null ) : MappingSchema . Default ;
261+ dataType = ms . GetDataType ( typeMapping . ClrType ) . Type . DataType ;
263262 }
264263 }
265264
@@ -278,7 +277,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
278277 {
279278 ( T ) ( Attribute ) new ColumnAttribute
280279 {
281- Name = prop . GetColumnName ( storeObjectId ! . Value ) ,
280+ Name = storeObjectId != null ? prop . GetColumnName ( storeObjectId . Value ) : null ,
282281 Length = prop . GetMaxLength ( ) ?? 0 ,
283282 CanBeNull = prop . IsNullable ,
284283 DbType = prop . GetColumnType ( ) ,
@@ -408,7 +407,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
408407 return Array . Empty < T > ( ) ;
409408 }
410409
411- class ValueConverter : IValueConverter
410+ sealed class ValueConverter : IValueConverter
412411 {
413412 public ValueConverter (
414413 LambdaExpression convertToProviderExpression ,
@@ -425,7 +424,7 @@ public ValueConverter(
425424
426425 }
427426
428- class SqlTransparentExpression : SqlExpression
427+ sealed class SqlTransparentExpression : SqlExpression
429428 {
430429 public Expression Expression { get ; }
431430
@@ -439,7 +438,7 @@ protected override void Print(ExpressionPrinter expressionPrinter)
439438 expressionPrinter . Print ( Expression ) ;
440439 }
441440
442- protected bool Equals ( SqlTransparentExpression other )
441+ private bool Equals ( SqlTransparentExpression other )
443442 {
444443 return ReferenceEquals ( this , other ) ;
445444 }
@@ -596,23 +595,24 @@ string PrepareExpressionText(Expression? expr)
596595 return text ;
597596 }
598597
598+ // https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG/Query/Expressions/Internal/PostgresBinaryExpression.cs
599599 if ( newExpression . GetType ( ) . Name == "PostgresBinaryExpression" )
600600 {
601601 // Handling NpgSql's PostgresBinaryExpression
602602
603- var left = newExpression . GetType ( ) . GetProperty ( "Left" ) ? . GetValue ( newExpression ) as Expression ;
604- var right = newExpression . GetType ( ) . GetProperty ( "Right" ) ? . GetValue ( newExpression ) as Expression ;
603+ var left = ( Expression ) newExpression . GetType ( ) . GetProperty ( "Left" ) ! . GetValue ( newExpression ) ! ;
604+ var right = ( Expression ) newExpression . GetType ( ) . GetProperty ( "Right" ) ! . GetValue ( newExpression ) ! ;
605605
606- var operand = newExpression . GetType ( ) . GetProperty ( "OperatorType" ) ? . GetValue ( newExpression ) ! . ToString ( ) ;
606+ var operand = newExpression . GetType ( ) . GetProperty ( "OperatorType" ) ! . GetValue ( newExpression ) ! . ToString ( ) ! ;
607607
608608 var operandExpr = operand switch
609609 {
610610 "Contains"
611- when left ! . Type . Name == "NpgsqlInetTypeMapping" ||
611+ when left . Type . Name == "NpgsqlInetTypeMapping" ||
612612 left . Type . Name == "NpgsqlCidrTypeMapping"
613613 => ">>" ,
614614 "ContainedBy"
615- when left ! . Type . Name == "NpgsqlInetTypeMapping" ||
615+ when left . Type . Name == "NpgsqlInetTypeMapping" ||
616616 left . Type . Name == "NpgsqlCidrTypeMapping"
617617 => "<<" ,
618618 "Contains" => "@>" ,
@@ -674,7 +674,7 @@ private static Expression UnwrapConverted(Expression expr)
674674 if ( expr is SqlFunctionExpression func )
675675 {
676676 if ( string . Equals ( func . Name , "COALESCE" , StringComparison . InvariantCultureIgnoreCase ) &&
677- func . Arguments ! . Count == 2 && func . Arguments [ 1 ] . NodeType == ExpressionType . Extension )
677+ func . Arguments ? . Count == 2 && func . Arguments [ 1 ] . NodeType == ExpressionType . Extension )
678678 return UnwrapConverted ( func . Arguments [ 0 ] ) ;
679679 }
680680
0 commit comments