-
-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SqlQuery with EF Core 8 and later
fixes #2062
- Loading branch information
Showing
5 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace #NAMESPACE# | ||
{ | ||
public static class DbContextExtensions | ||
{ | ||
public static async Task<List<T>> SqlQueryAsync<T>(this DbContext db, string sql, object[] parameters = null, CancellationToken cancellationToken = default) where T : class | ||
{ | ||
if (parameters is null) | ||
{ | ||
parameters = new object[] { }; | ||
} | ||
|
||
if (typeof(T).GetProperties().Any()) | ||
{ | ||
return await db.Database | ||
.SqlQueryRaw<T>(sql, parameters) | ||
.ToListAsync(cancellationToken); | ||
} | ||
else | ||
{ | ||
await db.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken); | ||
return default; | ||
} | ||
} | ||
} | ||
|
||
public class OutputParameter<TValue> | ||
{ | ||
private bool _valueSet = false; | ||
|
||
public TValue _value; | ||
|
||
public TValue Value | ||
{ | ||
get | ||
{ | ||
if (!_valueSet) | ||
throw new InvalidOperationException("Value not set."); | ||
|
||
return _value; | ||
} | ||
} | ||
|
||
internal void SetValue(object value) | ||
{ | ||
_valueSet = true; | ||
|
||
_value = null == value || Convert.IsDBNull(value) ? default(TValue) : (TValue)value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace #NAMESPACE# | ||
{ | ||
public static class DbContextExtensions | ||
{ | ||
public static List<T> SqlQuery<T>(this DbContext db, string sql, object[] parameters = null) where T : class | ||
{ | ||
if (parameters is null) | ||
{ | ||
parameters = new object[] { }; | ||
} | ||
|
||
if (typeof(T).GetProperties().Any()) | ||
{ | ||
return await db.Database | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
.SqlQueryRaw<T>(sql, parameters) | ||
.ToList(); | ||
} | ||
else | ||
{ | ||
db.Database.ExecuteSqlRaw(sql, parameters); | ||
return default; | ||
} | ||
} | ||
} | ||
|
||
public class OutputParameter<TValue> | ||
{ | ||
private bool _valueSet = false; | ||
|
||
public TValue _value; | ||
|
||
public TValue Value | ||
{ | ||
get | ||
{ | ||
if (!_valueSet) | ||
throw new InvalidOperationException("Value not set."); | ||
|
||
return _value; | ||
} | ||
} | ||
|
||
internal void SetValue(object value) | ||
{ | ||
_valueSet = true; | ||
|
||
_value = null == value || Convert.IsDBNull(value) ? default(TValue) : (TValue)value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Is it joke?