Skip to content

Commit 1ac6fb8

Browse files
author
zzzprojects
committed
Add better PostgreSQL support + Initial Migration EF Core v2
Add better PostgreSQL support + Initial Migration EF Core v2
1 parent 9eae405 commit 1ac6fb8

File tree

15 files changed

+635
-136
lines changed

15 files changed

+635
-136
lines changed

src/shared/Z.EF.Plus.BatchDelete.Shared/BatchDelete.cs

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -531,32 +531,69 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
531531
return null;
532532
}
533533
#else
534+
534535
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.StartsWith("Microsoft.EntityFrameworkCore.SqlServer", StringComparison.InvariantCulture));
536+
var postgreSqlAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.StartsWith("Npgsql.EntityFrameworkCore.PostgreSQL", StringComparison.InvariantCulture));
535537

536-
if (assembly != null)
538+
if (assembly != null || postgreSqlAssembly != null)
537539
{
538-
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
539-
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IEntityType) }, null);
540-
var sqlServerPropertyMethod = type.GetMethod("SqlServer", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IProperty) }, null);
541-
var sqlServer = (IRelationalEntityTypeAnnotations)sqlServerEntityTypeMethod.Invoke(null, new[] { entity });
540+
string tableName = "";
541+
var columnKeys = new List<string>();
542+
string primaryKeys = "";
542543

543-
// GET mapping
544-
var tableName = string.IsNullOrEmpty(sqlServer.Schema) ?
545-
string.Concat("[", sqlServer.TableName, "]") :
546-
string.Concat("[", sqlServer.Schema, "].[", sqlServer.TableName, "]");
544+
if (assembly != null)
545+
{
546+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
547+
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IEntityType) }, null);
548+
var sqlServerPropertyMethod = type.GetMethod("SqlServer", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IProperty) }, null);
549+
var sqlServer = (IRelationalEntityTypeAnnotations)sqlServerEntityTypeMethod.Invoke(null, new[] { entity });
547550

548-
// GET keys mappings
549-
var columnKeys = new List<string>();
550-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
551+
// GET mapping
552+
tableName = string.IsNullOrEmpty(sqlServer.Schema) ?
553+
string.Concat("[", sqlServer.TableName, "]") :
554+
string.Concat("[", sqlServer.Schema, "].[", sqlServer.TableName, "]");
555+
556+
// GET keys mappings
557+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
558+
{
559+
var mappingProperty = sqlServerPropertyMethod.Invoke(null, new[] { propertyKey });
560+
561+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
562+
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
563+
}
564+
565+
// GET primary key join
566+
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.[", x, "] = B.[", x, "]")));
567+
}
568+
else if (postgreSqlAssembly != null)
551569
{
552-
var mappingProperty = sqlServerPropertyMethod.Invoke(null, new[] { propertyKey });
570+
var type = postgreSqlAssembly.GetType("Microsoft.EntityFrameworkCore.NpgsqlMetadataExtensions");
571+
var sqlServerEntityTypeMethod = type.GetMethod("Npgsql", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IEntityType) }, null);
572+
var sqlServerPropertyMethod = type.GetMethod("Npgsql", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(IProperty) }, null);
573+
var sqlServer = (IRelationalEntityTypeAnnotations)sqlServerEntityTypeMethod.Invoke(null, new[] { entity });
574+
575+
// GET mapping
576+
tableName = string.IsNullOrEmpty(sqlServer.Schema) ?
577+
string.Concat("\"", sqlServer.TableName, "\"") :
578+
string.Concat("\"", sqlServer.Schema, "\".\"", sqlServer.TableName, "\"");
579+
580+
// GET keys mappings
581+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
582+
{
583+
var mappingProperty = sqlServerPropertyMethod.Invoke(null, new[] { propertyKey });
553584

554-
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
555-
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
585+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
586+
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
587+
}
588+
589+
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.\"", x, "\" = B.\"", x, "\"")));
556590
}
557591

592+
558593
// GET command text template
559-
var commandTextTemplate = BatchSize > 0 ?
594+
var commandTextTemplate = assembly == null && postgreSqlAssembly != null ?
595+
CommandTextPostgreSQLTemplate :
596+
BatchSize > 0 ?
560597
BatchDelayInterval > 0 ?
561598
CommandTextWhileDelayTemplate :
562599
CommandTextWhileTemplate :
@@ -571,8 +608,6 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
571608
#endif
572609
var querySelect = relationalCommand.CommandText;
573610

574-
// GET primary key join
575-
var primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.[", x, "] = B.[", x, "]")));
576611

577612
// REPLACE template
578613
commandTextTemplate = commandTextTemplate.Replace("{TableName}", tableName)

0 commit comments

Comments
 (0)