Skip to content

Commit

Permalink
Use SqlQuery with EF Core 8 and later
Browse files Browse the repository at this point in the history
fixes #2062
  • Loading branch information
ErikEJ committed Feb 17, 2024
1 parent ad83240 commit cff9af0
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ protected override string WriteDbContext(ModuleScaffolderOptions scaffolderOptio
}

Sb.AppendLine("}");
#if !CORE80
GenerateOnModelCreating(model);
#endif
}

Sb.AppendLine("}");
Expand Down Expand Up @@ -191,7 +193,11 @@ protected override string WriteDbContextInterface(ModuleScaffolderOptions scaffo

private static string GetDbContextExtensionsText(bool useAsyncCalls)
{
#if CORE80
var dbContextExtensionTemplateName = useAsyncCalls ? "RevEng.Core.DbContextExtensionsSqlQuery" : "RevEng.Core.DbContextExtensionsSqlQuery.Sync";
#else
var dbContextExtensionTemplateName = useAsyncCalls ? "RevEng.Core.DbContextExtensions" : "RevEng.Core.DbContextExtensions.Sync";
#endif
var assembly = typeof(SqlServerStoredProcedureScaffolder).GetTypeInfo().Assembly;
using Stream stream = assembly.GetManifestResourceStream(dbContextExtensionTemplateName);
if (stream == null)
Expand Down Expand Up @@ -542,7 +548,7 @@ private void GenerateProcedure(Routine procedure, RoutineModel model, bool signa
Sb.AppendLine("}");
}
}

#if !CORE80
private void GenerateOnModelCreating(RoutineModel model)
{
Sb.AppendLine();
Expand Down Expand Up @@ -577,7 +583,7 @@ private void GenerateOnModelCreating(RoutineModel model)

Sb.AppendLine("}");
}

#endif
private void GenerateParameterVar(ModuleParameter parameter, Routine procedure)
{
Sb.Append($"var {ParameterPrefix}{parameter.Name} = ");
Expand Down
61 changes: 61 additions & 0 deletions src/Core/RevEng.Core.80/DbContextExtensionsSqlQuery
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;
}
}
}
61 changes: 61 additions & 0 deletions src/Core/RevEng.Core.80/DbContextExtensionsSqlQuery.Sync
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.

Copy link
@ServioSoft

ServioSoft Mar 26, 2024

Is it joke?

This comment has been minimized.

Copy link
@ErikEJ

ErikEJ Mar 26, 2024

Author Owner

More like a bug 😄

.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;
}
}
}
6 changes: 3 additions & 3 deletions src/Core/RevEng.Core.80/RevEng.Core.80.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\RevEng.Core.60\DbContextExtensions" Link="DbContextExtensions" />
<EmbeddedResource Include="..\RevEng.Core.60\DbContextExtensions.Sync" Link="DbContextExtensions.Sync" />
</ItemGroup>
<EmbeddedResource Include="DbContextExtensionsSqlQuery" />
<EmbeddedResource Include="DbContextExtensionsSqlQuery.Sync" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Bricelam.EntityFrameworkCore.Pluralizer" Version="1.0.0" />
Expand Down
Binary file modified src/GUI/lib/efreveng80.exe.zip
Binary file not shown.

0 comments on commit cff9af0

Please sign in to comment.