To make the story short, i have a model like this.
public class User : IEntity
{
public int Id { get; set; }
public string Properties { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
[NotMapped]
public UserDTO _Properties
{
get
{
return Properties == null ? null : JsonConvert.DeserializeObject<UserDTO>(Properties);
}
set
{
Properties = JsonConvert.SerializeObject(value);
}
}
}
And im storing json data inside the properties column, that part is ok, then i have a query where i need to search by Name or Email, so i have a Repository pattern and
alter procedure [dbo].[spSearchUsers]
@term nvarchar(max) AS BEGIN
select Id, JSON_VALUE(Properties, '$.FirstName') as FirstName,
JSON_VALUE(Properties, '$.LastName') as LastName,
JSON_VALUE(Properties, '$.Email') as Email,
JSON_VALUE(Properties, '$.Role') As [Role],
JSON_VALUE(Properties, '$.ProgramInformation') as ProgramInformation,
JSON_VALUE(Properties, '$.CertsAndCredentials') as CertsAndCredentials,
JSON_VALUE(Properties, '$.Phone') as Phone,
JSON_VALUE(Properties, '$.LogoUrl') as LogoUrl,
JSON_VALUE(Properties, '$.ProfessionalPic') as ProfessionalPic,
JSON_VALUE(Properties, '$.Biography') as Biography,
CreatedAt, UpdatedAt
from Users
where CONCAT(JSON_VALUE(Properties, '$.FirstName'),' ',JSON_VALUE(Properties, '$.LastName')) like '%'+ @term +'%'
or JSON_VALUE(Properties, '$.Email') like '%'+ @term +'%'
and JSON_VALUE(Properties, '$.IsActive') = 'true'
and JSON_VALUE(Properties, '$.IsLockedOut') = 'false'
and JSON_VALUE(Properties, '$.IsVerified') = 'true' END
When i execute the stored procedure inside the sql server management studio i get the information right:
exec spSearchUsers 'Raul'
I get this result:
1 Raul Alejandro Baez Camarillo [email protected] 0 NULL NULL NULL NULL NULL NULL 2021-11-16 03:08:09.6630000 2021-11-16 03:08:09.6630000
So now i want to consume that stored procedure in my controller, im using Repository pattern and i have tried using the context.Model.FromSqlRaw like this.
var result = await _context.Users.FromSqlRaw("EXEC dbo.spSearchUsers {0}", term).ToListAsync()
But when i execute it im getting this error:
System.InvalidOperationException: The required column 'Properties' was not present in the results of a 'FromSql' operation. at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable
1.BuildIndexMap(IReadOnlyList1 columnNames, DbDataReader dataReader) at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) at AcadminRest.Controllers.UsersController.Search(String term) in D:\Projects\Accomplishment\AcadminRest\AcadminRest\Controllers\UsersController.cs:line 71 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
Can someone help me getting this? thanks in advance.