Skip to content

Commit 7710bf6

Browse files
author
zzzprojects
committed
Fix issue with false expression for UpdateFromQuery && DeleteFromQuery
Fix issue with false expression for UpdateFromQuery && DeleteFromQuery
1 parent a8fe094 commit 7710bf6

File tree

40 files changed

+568
-93
lines changed

40 files changed

+568
-93
lines changed

src/Z.EntityFramework.Plus.EF5.NET40/BatchDelete/BatchDelete.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public BatchDelete()
126126
/// <returns>The number of rows affected.</returns>
127127
public int Execute<T>(IQueryable<T> query) where T : class
128128
{
129+
if (query.Expression.ToString().Contains(".Where(x => False)"))
130+
{
131+
return 0;
132+
}
133+
129134
// GET model and info
130135
#if EF5 || EF6
131136
var model = query.GetDbContext().GetModel();
@@ -135,7 +140,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
135140
// SELECT keys names
136141
var queryKeys = query.SelectByName(keys.Select(x => x.Name).ToList());
137142
var innerObjectQuery = queryKeys.GetObjectQuery();
138-
143+
139144
// CREATE command
140145
var command = CreateCommand(innerObjectQuery, entity);
141146

@@ -187,6 +192,16 @@ public int Execute<T>(IQueryable<T> query) where T : class
187192
}
188193
}
189194
#elif EFCORE
195+
if (BatchDeleteManager.InMemoryDbContextFactory != null && query.IsInMemoryQueryContext())
196+
{
197+
var context = BatchDeleteManager.InMemoryDbContextFactory();
198+
199+
var list = query.ToList();
200+
context.RemoveRange(list);
201+
context.SaveChanges();
202+
return list.Count;
203+
}
204+
190205
var dbContext = query.GetDbContext();
191206
var entity = dbContext.Model.FindEntityType(typeof(T));
192207
var keys = entity.GetKeys().ToList()[0].Properties;
@@ -296,14 +311,23 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
296311
#elif EFCORE
297312
public DbCommand CreateCommand(IQueryable query, IEntityType entity)
298313
{
299-
#if NETCORE50
314+
#if NETSTANDARD1_3
300315
try
301316
{
302-
var assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
317+
Assembly assembly;
318+
319+
try
320+
{
321+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
322+
}
323+
catch (Exception ex)
324+
{
325+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
326+
}
303327

304328
if (assembly != null)
305329
{
306-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
330+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
307331

308332
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", new[] {typeof (IEntityType)});
309333
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});

src/Z.EntityFramework.Plus.EF5.NET40/BatchDelete/BatchDeleteManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System;
9+
#if EFCORE
10+
using Microsoft.EntityFrameworkCore;
11+
#endif
912

1013
namespace Z.EntityFramework.Plus
1114
{
@@ -15,5 +18,12 @@ public class BatchDeleteManager
1518
/// <summary>Gets or sets the batch delete builder to change default configuration.</summary>
1619
/// <value>The batch delete builder to change default configuration.</value>
1720
public static Action<BatchDelete> BatchDeleteBuilder { get; set; }
21+
22+
#if EFCORE
23+
24+
/// <summary>Gets or sets the factory to create an InMemory DbContext.</summary>
25+
/// <value>The factory to create an InMemory DbContext.</value>
26+
public static Func<DbContext> InMemoryDbContextFactory { get; set; }
27+
#endif
1828
}
1929
}

src/Z.EntityFramework.Plus.EF5.NET40/BatchUpdate/BatchUpdate.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public BatchUpdate()
120120
/// <returns>The number of rows affected.</returns>
121121
public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : class
122122
{
123+
if (query.Expression.ToString().Contains(".Where(x => False)"))
124+
{
125+
return 0;
126+
}
127+
123128
#if EF5 || EF6
124129
var objectQuery = query.GetObjectQuery();
125130

@@ -301,12 +306,21 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
301306
#elif EFCORE
302307
public DbCommand CreateCommand(IQueryable query, IEntityType entity, List<Tuple<string, object>> values)
303308
{
304-
#if NETCORE50
305-
var assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
309+
#if NETSTANDARD1_3
310+
Assembly assembly;
311+
312+
try
313+
{
314+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
315+
}
316+
catch (Exception ex)
317+
{
318+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
319+
}
306320

307321
if (assembly != null)
308322
{
309-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
323+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
310324
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", new[] {typeof (IEntityType)});
311325
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});
312326
var sqlServer = (IRelationalEntityTypeAnnotations) sqlServerEntityTypeMethod.Invoke(null, new[] {entity});
@@ -442,31 +456,31 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
442456
var mapping = entity.Info.EntityTypeMapping.MappingFragment;
443457
#elif EFCORE
444458

445-
#if NETCORE50
459+
#if NETSTANDARD1_3
446460
Assembly assembly;
447461

448462
try
449463
{
450-
assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
464+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
451465
}
452-
catch (Exception)
466+
catch (Exception ex)
453467
{
454-
throw new Exception("");
468+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
455469
}
456470

457471
if (assembly == null)
458472
{
459-
throw new Exception("");
473+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
460474
}
461475

462-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
476+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
463477
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});
464478
#else
465479
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.StartsWith("Microsoft.EntityFrameworkCore.SqlServer"));
466480

467481
if (assembly == null)
468482
{
469-
throw new Exception("");
483+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
470484
}
471485

472486
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
@@ -538,7 +552,7 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
538552
var command = ((IQueryable) result).CreateCommand(out queryContext);
539553
var commandText = command.CommandText;
540554

541-
#if NETCORE50
555+
#if NETSTANDARD1_3
542556
// GET the 'value' part
543557
var valueSql = commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.CurrentCultureIgnoreCase) != -1 ?
544558
commandText.Substring(6, commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.CurrentCultureIgnoreCase) - 6) :

src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("e4c2af73-caeb-4429-bcb6-0a359484e064")]
21-
[assembly: AssemblyVersion("1.3.6")]
22-
[assembly: AssemblyFileVersion("1.3.6")]
21+
[assembly: AssemblyVersion("1.3.9")]
22+
[assembly: AssemblyFileVersion("1.3.9")]

src/Z.EntityFramework.Plus.EF5/BatchDelete/BatchDelete.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public BatchDelete()
126126
/// <returns>The number of rows affected.</returns>
127127
public int Execute<T>(IQueryable<T> query) where T : class
128128
{
129+
if (query.Expression.ToString().Contains(".Where(x => False)"))
130+
{
131+
return 0;
132+
}
133+
129134
// GET model and info
130135
#if EF5 || EF6
131136
var model = query.GetDbContext().GetModel();
@@ -135,7 +140,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
135140
// SELECT keys names
136141
var queryKeys = query.SelectByName(keys.Select(x => x.Name).ToList());
137142
var innerObjectQuery = queryKeys.GetObjectQuery();
138-
143+
139144
// CREATE command
140145
var command = CreateCommand(innerObjectQuery, entity);
141146

@@ -187,6 +192,16 @@ public int Execute<T>(IQueryable<T> query) where T : class
187192
}
188193
}
189194
#elif EFCORE
195+
if (BatchDeleteManager.InMemoryDbContextFactory != null && query.IsInMemoryQueryContext())
196+
{
197+
var context = BatchDeleteManager.InMemoryDbContextFactory();
198+
199+
var list = query.ToList();
200+
context.RemoveRange(list);
201+
context.SaveChanges();
202+
return list.Count;
203+
}
204+
190205
var dbContext = query.GetDbContext();
191206
var entity = dbContext.Model.FindEntityType(typeof(T));
192207
var keys = entity.GetKeys().ToList()[0].Properties;
@@ -296,14 +311,23 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
296311
#elif EFCORE
297312
public DbCommand CreateCommand(IQueryable query, IEntityType entity)
298313
{
299-
#if NETCORE50
314+
#if NETSTANDARD1_3
300315
try
301316
{
302-
var assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
317+
Assembly assembly;
318+
319+
try
320+
{
321+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
322+
}
323+
catch (Exception ex)
324+
{
325+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
326+
}
303327

304328
if (assembly != null)
305329
{
306-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
330+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
307331

308332
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", new[] {typeof (IEntityType)});
309333
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});

src/Z.EntityFramework.Plus.EF5/BatchDelete/BatchDeleteManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System;
9+
#if EFCORE
10+
using Microsoft.EntityFrameworkCore;
11+
#endif
912

1013
namespace Z.EntityFramework.Plus
1114
{
@@ -15,5 +18,12 @@ public class BatchDeleteManager
1518
/// <summary>Gets or sets the batch delete builder to change default configuration.</summary>
1619
/// <value>The batch delete builder to change default configuration.</value>
1720
public static Action<BatchDelete> BatchDeleteBuilder { get; set; }
21+
22+
#if EFCORE
23+
24+
/// <summary>Gets or sets the factory to create an InMemory DbContext.</summary>
25+
/// <value>The factory to create an InMemory DbContext.</value>
26+
public static Func<DbContext> InMemoryDbContextFactory { get; set; }
27+
#endif
1828
}
1929
}

src/Z.EntityFramework.Plus.EF5/BatchUpdate/BatchUpdate.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public BatchUpdate()
120120
/// <returns>The number of rows affected.</returns>
121121
public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : class
122122
{
123+
if (query.Expression.ToString().Contains(".Where(x => False)"))
124+
{
125+
return 0;
126+
}
127+
123128
#if EF5 || EF6
124129
var objectQuery = query.GetObjectQuery();
125130

@@ -301,12 +306,21 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
301306
#elif EFCORE
302307
public DbCommand CreateCommand(IQueryable query, IEntityType entity, List<Tuple<string, object>> values)
303308
{
304-
#if NETCORE50
305-
var assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
309+
#if NETSTANDARD1_3
310+
Assembly assembly;
311+
312+
try
313+
{
314+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
315+
}
316+
catch (Exception ex)
317+
{
318+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
319+
}
306320

307321
if (assembly != null)
308322
{
309-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
323+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
310324
var sqlServerEntityTypeMethod = type.GetMethod("SqlServer", new[] {typeof (IEntityType)});
311325
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});
312326
var sqlServer = (IRelationalEntityTypeAnnotations) sqlServerEntityTypeMethod.Invoke(null, new[] {entity});
@@ -442,31 +456,31 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
442456
var mapping = entity.Info.EntityTypeMapping.MappingFragment;
443457
#elif EFCORE
444458

445-
#if NETCORE50
459+
#if NETSTANDARD1_3
446460
Assembly assembly;
447461

448462
try
449463
{
450-
assembly = Assembly.Load(new AssemblyName("EntityFramework.MicrosoftSqlServer, Version = 7.0.0.0, Culture = neutral, PublicKeyToken = adb9793829ddae60"));
464+
assembly = Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.SqlServer"));
451465
}
452-
catch (Exception)
466+
catch (Exception ex)
453467
{
454-
throw new Exception("");
468+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
455469
}
456470

457471
if (assembly == null)
458472
{
459-
throw new Exception("");
473+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
460474
}
461475

462-
var type = assembly.GetType("Microsoft.Data.Entity.SqlServerMetadataExtensions");
476+
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
463477
var sqlServerPropertyMethod = type.GetMethod("SqlServer", new[] {typeof (IProperty)});
464478
#else
465479
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.StartsWith("Microsoft.EntityFrameworkCore.SqlServer"));
466480

467481
if (assembly == null)
468482
{
469-
throw new Exception("");
483+
throw new Exception(ExceptionMessage.BatchOperations_AssemblyNotFound);
470484
}
471485

472486
var type = assembly.GetType("Microsoft.EntityFrameworkCore.SqlServerMetadataExtensions");
@@ -538,7 +552,7 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
538552
var command = ((IQueryable) result).CreateCommand(out queryContext);
539553
var commandText = command.CommandText;
540554

541-
#if NETCORE50
555+
#if NETSTANDARD1_3
542556
// GET the 'value' part
543557
var valueSql = commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.CurrentCultureIgnoreCase) != -1 ?
544558
commandText.Substring(6, commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.CurrentCultureIgnoreCase) - 6) :

src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("abcbb878-043c-4957-a334-90e9872e684e")]
21-
[assembly: AssemblyVersion("1.3.6")]
22-
[assembly: AssemblyFileVersion("1.3.6")]
21+
[assembly: AssemblyVersion("1.3.9")]
22+
[assembly: AssemblyFileVersion("1.3.9")]

0 commit comments

Comments
 (0)