1

I'm currently porting over my project from the .NET Framework to .NET Standard 2.0, and will be feeding the nuget packages to a .NET Core application. With that said I'm currently having issues on handling this last issue which is that i'm unable to use the SqlQuery method using the .NET Standard framework. Below is the error i'm receiving as well as the libraries being used and where I was originally using the SqlQuery method. I have seen various implementations with FromSql however I don't believe this works for me because I want to return an int and FromSql returns TEntity.


'DatabaseFacade' does not contain a definition for 'SqlQuery' and no accessible extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)


using Microservices.LibCore.Core;
using Microservices.LibCore.Core.Base.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Reflection;

public static List<object> setLegacyKey(DbContext dbContext, ServiceObject obj)
        {
            var keyValuesFound = new List<object>();
            var keyProperties = getKeyProperties(obj);

            foreach (var key in keyProperties)
            {
                string sql = "select " + getKeyColumnName(key) + " from " + getTableName(obj) + " where global_id=@p0";

                List<object> result = dbContext.Database.SqlQuery(key.PropertyType, sql, obj.Id).ToListAsync().Result;

                if (result.Count == 0)
                {
                    throw new ServiceException(ServiceError.ERROR_CODE.INVALID_REQUEST, String.Format("Invalid {1}.ID ({0})", obj.Id, obj.GetType().Name));
                }

                var keyValue = result.FirstOrDefault();

                key.SetValue(obj, keyValue);

                keyValuesFound.Add(keyValue);
            }

            return keyValuesFound;
        }
Danny
  • 11
  • 4
  • Have a look at someone with a similar issue here: https://stackoverflow.com/questions/40428158/execute-non-query-procedure-not-working-asp-net-core – Tom John Oct 18 '18 at 16:57
  • Possible duplicate of [Execute non-query procedure not working asp.net core](https://stackoverflow.com/questions/40428158/execute-non-query-procedure-not-working-asp-net-core) – Tom John Oct 18 '18 at 16:59
  • Just don't use EF for this. A micro-ORM like Dapper is a lot easier for such tasks; it only takes a few lines to achieve what you want. Running raw SQL queries is never a core activity of full OR/mappers. – Gert Arnold Oct 19 '18 at 08:50

0 Answers0