@@ -444,110 +444,48 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
444444 public DbCommand CreateCommand ( IQueryable query , IEntityType entity )
445445 {
446446#if NETSTANDARD1_3
447+ Assembly assembly = null ;
448+ Assembly postgreSqlAssembly = null ;
449+ Assembly mySqlPomelo = null ;
450+ Assembly mySql = null ;
451+
447452 try
448453 {
449- Assembly assembly ;
450-
454+ assembly = Assembly . Load ( new AssemblyName ( "Microsoft.EntityFrameworkCore.SqlServer" ) ) ;
455+ }
456+ catch ( Exception ex )
457+ {
451458 try
452459 {
453- assembly = Assembly . Load ( new AssemblyName ( "Microsoft.EntityFrameworkCore.SqlServer" ) ) ;
454- }
455- catch ( Exception ex )
456- {
457- throw new Exception ( ExceptionMessage . BatchOperations_AssemblyNotFound ) ;
460+ postgreSqlAssembly = Assembly . Load ( new AssemblyName ( "Npgsql.EntityFrameworkCore.PostgreSQL" ) ) ;
458461 }
459-
460- if ( assembly != null )
462+ catch
461463 {
462- var type = assembly . GetType ( "Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions" ) ;
463-
464- var sqlServerEntityTypeMethod = type . GetMethod ( "SqlServer" , new [ ] { typeof ( IEntityType ) } ) ;
465- var sqlServerPropertyMethod = type . GetMethod ( "SqlServer" , new [ ] { typeof ( IProperty ) } ) ;
466- var sqlServer = ( IRelationalEntityTypeAnnotations ) sqlServerEntityTypeMethod . Invoke ( null , new [ ] { entity } ) ;
467-
468- // GET mapping
469- var tableName = string . IsNullOrEmpty ( sqlServer . Schema ) ?
470- string . Concat ( "[" , sqlServer . TableName , "]" ) :
471- string . Concat ( "[" , sqlServer . Schema , "].[" , sqlServer . TableName , "]" ) ;
472-
473- // GET keys mappings
474- var columnKeys = new List < string > ( ) ;
475- foreach ( var propertyKey in entity . GetKeys ( ) . ToList ( ) [ 0 ] . Properties )
464+ try
476465 {
477- var mappingProperty = sqlServerPropertyMethod . Invoke ( null , new [ ] { propertyKey } ) ;
478-
479- var columnNameProperty = mappingProperty . GetType ( ) . GetProperty ( "ColumnName" , BindingFlags . Public | BindingFlags . Instance ) ;
480- columnKeys . Add ( ( string ) columnNameProperty . GetValue ( mappingProperty ) ) ;
466+ mySql = Assembly . Load ( new AssemblyName ( "MySql.Data.EntityFrameworkCore" ) ) ;
467+ }
468+ catch
469+ {
470+ try
471+ {
472+ mySqlPomelo = Assembly . Load ( new AssemblyName ( "Pomelo.EntityFrameworkCore.MySql" ) ) ;
473+ }
474+ catch
475+ {
476+ throw new Exception ( ExceptionMessage . BatchOperations_AssemblyNotFound ) ;
477+ }
481478 }
482-
483- // GET command text template
484- var commandTextTemplate = BatchSize > 0 ?
485- BatchDelayInterval > 0 ?
486- CommandTextWhileDelayTemplate :
487- CommandTextWhileTemplate :
488- CommandTextTemplate ;
489-
490- // GET inner query
491- #if EFCORE
492- RelationalQueryContext queryContext ;
493- var relationalCommand = query . CreateCommand ( out queryContext ) ;
494- #else
495- var relationalCommand = query . CreateCommand ( ) ;
496- #endif
497-
498- var querySelect = relationalCommand . CommandText ;
499-
500- // GET primary key join
501- var primaryKeys = string . Join ( Environment . NewLine + "AND " , columnKeys . Select ( x => string . Concat ( "A.[" , x , "] = B.[" , x , "]" ) ) ) ;
502-
503- // REPLACE template
504- commandTextTemplate = commandTextTemplate . Replace ( "{TableName}" , tableName )
505- . Replace ( "{Select}" , querySelect )
506- . Replace ( "{PrimaryKeys}" , primaryKeys )
507- . Replace ( "{Top}" , BatchSize . ToString ( ) )
508- . Replace ( "{Delay}" , TimeSpan . FromMilliseconds ( BatchDelayInterval ) . ToString ( @"hh\:mm\:ss\:fff" ) ) ;
509-
510- // CREATE command
511- var command = query . GetDbContext ( ) . CreateStoreCommand ( ) ;
512- command . CommandText = commandTextTemplate ;
513-
514- #if EFCORE
515- // ADD Parameter
516- foreach ( var relationalParameter in relationalCommand . Parameters )
517- {
518- var parameter = queryContext . ParameterValues [ relationalParameter . InvariantName ] ;
519-
520- var param = command . CreateParameter ( ) ;
521- param . CopyFrom ( relationalParameter , parameter ) ;
522-
523- command . Parameters . Add ( param ) ;
524- }
525- #else
526- // ADD Parameter
527- var parameterCollection = relationalCommand . Parameters ;
528- foreach ( var parameter in parameterCollection )
529- {
530- var param = command . CreateParameter ( ) ;
531- param . CopyFrom ( parameter ) ;
532-
533- command . Parameters . Add ( param ) ;
534- }
535- #endif
536-
537- return command ;
538479 }
539- return null ;
540- }
541- catch ( Exception )
542- {
543- return null ;
544480 }
545481#else
546482
547483 var assembly = AppDomain . CurrentDomain . GetAssemblies ( ) . FirstOrDefault ( x => x . FullName . StartsWith ( "Microsoft.EntityFrameworkCore.SqlServer" , StringComparison . InvariantCulture ) ) ;
548484 var postgreSqlAssembly = AppDomain . CurrentDomain . GetAssemblies ( ) . FirstOrDefault ( x => x . FullName . StartsWith ( "Npgsql.EntityFrameworkCore.PostgreSQL" , StringComparison . InvariantCulture ) ) ;
549-
550- if ( assembly != null || postgreSqlAssembly != null )
485+ var mySqlPomelo = AppDomain . CurrentDomain . GetAssemblies ( ) . FirstOrDefault ( x => x . FullName . StartsWith ( "Pomelo.EntityFrameworkCore.MySql" , StringComparison . InvariantCulture ) ) ;
486+ var mySql = AppDomain . CurrentDomain . GetAssemblies ( ) . FirstOrDefault ( x => x . FullName . StartsWith ( "MySql.Data.EntityFrameworkCore" , StringComparison . InvariantCulture ) ) ;
487+ #endif
488+ if ( assembly != null || postgreSqlAssembly != null || mySqlPomelo != null || mySql != null )
551489 {
552490 string tableName = "" ;
553491 var columnKeys = new List < string > ( ) ;
@@ -556,8 +494,8 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
556494 if ( assembly != null )
557495 {
558496 var type = assembly . GetType ( "Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions" ) ;
559- var sqlServerEntityTypeMethod = type . GetMethod ( "SqlServer" , BindingFlags . Public | BindingFlags . Static , null , new [ ] { typeof ( IEntityType ) } , null ) ;
560- var sqlServerPropertyMethod = type . GetMethod ( "SqlServer" , BindingFlags . Public | BindingFlags . Static , null , new [ ] { typeof ( IProperty ) } , null ) ;
497+ var sqlServerEntityTypeMethod = type . GetMethod ( "SqlServer" , new [ ] { typeof ( IEntityType ) } ) ;
498+ var sqlServerPropertyMethod = type . GetMethod ( "SqlServer" , new [ ] { typeof ( IProperty ) } ) ;
561499 var sqlServer = ( IRelationalEntityTypeAnnotations ) sqlServerEntityTypeMethod . Invoke ( null , new [ ] { entity } ) ;
562500
563501 // GET mapping
@@ -580,8 +518,8 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
580518 else if ( postgreSqlAssembly != null )
581519 {
582520 var type = postgreSqlAssembly . GetType ( "Microsoft.EntityFrameworkCore.NpgsqlMetadataExtensions" ) ;
583- var sqlServerEntityTypeMethod = type . GetMethod ( "Npgsql" , BindingFlags . Public | BindingFlags . Static , null , new [ ] { typeof ( IEntityType ) } , null ) ;
584- var sqlServerPropertyMethod = type . GetMethod ( "Npgsql" , BindingFlags . Public | BindingFlags . Static , null , new [ ] { typeof ( IProperty ) } , null ) ;
521+ var sqlServerEntityTypeMethod = type . GetMethod ( "Npgsql" , new [ ] { typeof ( IEntityType ) } ) ;
522+ var sqlServerPropertyMethod = type . GetMethod ( "Npgsql" , new [ ] { typeof ( IProperty ) } ) ;
585523 var sqlServer = ( IRelationalEntityTypeAnnotations ) sqlServerEntityTypeMethod . Invoke ( null , new [ ] { entity } ) ;
586524
587525 // GET mapping
@@ -600,11 +538,55 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
600538
601539 primaryKeys = string . Join ( Environment . NewLine + "AND " , columnKeys . Select ( x => string . Concat ( "A.\" " , x , "\" = B.\" " , x , "\" " ) ) ) ;
602540 }
541+ else if ( mySqlPomelo != null )
542+ {
543+ var type = mySqlPomelo . GetType ( "Microsoft.EntityFrameworkCore.MySqlMetadataExtensions" ) ;
544+ var sqlServerEntityTypeMethod = type . GetMethod ( "MySql" , new [ ] { typeof ( IEntityType ) } ) ;
545+ var sqlServerPropertyMethod = type . GetMethod ( "MySql" , new [ ] { typeof ( IProperty ) } ) ;
546+ var sqlServer = ( IRelationalEntityTypeAnnotations ) sqlServerEntityTypeMethod . Invoke ( null , new [ ] { entity } ) ;
547+
548+ // GET mapping
549+ tableName = string . Concat ( "`" , sqlServer . TableName , "`" ) ;
550+
551+ // GET keys mappings
552+ foreach ( var propertyKey in entity . GetKeys ( ) . ToList ( ) [ 0 ] . Properties )
553+ {
554+ var mappingProperty = sqlServerPropertyMethod . Invoke ( null , new [ ] { propertyKey } ) ;
555+
556+ var columnNameProperty = mappingProperty . GetType ( ) . GetProperty ( "ColumnName" , BindingFlags . Public | BindingFlags . Instance ) ;
557+ columnKeys . Add ( ( string ) columnNameProperty . GetValue ( mappingProperty ) ) ;
558+ }
559+
560+ primaryKeys = string . Join ( Environment . NewLine + "AND " , columnKeys . Select ( x => string . Concat ( "A.`" , x , "` = B.`" , x , "`" ) ) ) ;
561+ }
562+ else if ( mySql != null )
563+ {
564+ var type = mySql . GetType ( "MySQL.Data.EntityFrameworkCore.MySQLMetadataExtensions" ) ;
565+ var sqlServerEntityTypeMethod = type . GetMethod ( "MySQL" , new [ ] { typeof ( IEntityType ) } ) ;
566+ var sqlServerPropertyMethod = type . GetMethod ( "MySQL" , new [ ] { typeof ( IProperty ) } ) ;
567+ var sqlServer = ( IRelationalEntityTypeAnnotations ) sqlServerEntityTypeMethod . Invoke ( null , new [ ] { entity } ) ;
568+
569+ // GET mapping
570+ tableName = string . Concat ( "`" , sqlServer . TableName , "`" ) ;
571+
572+ // GET keys mappings
573+ foreach ( var propertyKey in entity . GetKeys ( ) . ToList ( ) [ 0 ] . Properties )
574+ {
575+ var mappingProperty = sqlServerPropertyMethod . Invoke ( null , new [ ] { propertyKey } ) ;
576+
577+ var columnNameProperty = mappingProperty . GetType ( ) . GetProperty ( "ColumnName" , BindingFlags . Public | BindingFlags . Instance ) ;
578+ columnKeys . Add ( ( string ) columnNameProperty . GetValue ( mappingProperty ) ) ;
579+ }
580+
581+ primaryKeys = string . Join ( Environment . NewLine + "AND " , columnKeys . Select ( x => string . Concat ( "A.`" , x , "` = B.`" , x , "`" ) ) ) ;
582+ }
603583
604584
605585 // GET command text template
606586 var commandTextTemplate = assembly == null && postgreSqlAssembly != null ?
607587 CommandTextPostgreSQLTemplate :
588+ mySqlPomelo != null || mySql != null ?
589+ CommandTextTemplate_MySql :
608590 BatchSize > 0 ?
609591 BatchDelayInterval > 0 ?
610592 CommandTextWhileDelayTemplate :
@@ -658,7 +640,6 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
658640 return command ;
659641 }
660642 return null ;
661- #endif
662643 }
663644#endif
664645 public string EscapeName ( string name , bool isMySql , bool isOracle )
0 commit comments