Provést postup a vrátit data z různých tabulek v Net 5

0

Otázka

Snažím se spustit proceduru v Rest API s NET 5 a Entity Framework Core 5, procedura vrací data z různých tabulek.

Problém mám, je, že nevím, jak provést postup, aby se později vrátit data, v DetailsClientsDto třídy.

Zkuste spustit postup v následující způsoby, ale bez úspěchu:

var result = await _context.Database.SqlQuery<DetailsClientsDto>("EXEC [dbo].[SPROC_DETAILS] @ID_USER", sqlParameters);

var result = await _context.SqlQuery<DetailsClientsDto>("EXEC [dbo].[SPROC_DETAILS] @ID_USER", sqlParameters);

Chyba:

DataBase facade does not contain a definition for SqlQuery. Is there a using directive missing?

Metodu používám je:

private readonly MarketContext _context;
public ClientsRepository(MarketContext context) : base(context)
{
    _context = context;
}

public async Task<DetailsClientsDto> GetDetailsRepository(SearchDetailsDto details)
{
    var sqlParameters = new[]
    {
        new SqlParameter
        {
            ParameterName = "ID_USER",
            Value = details.IdUser,
            SqlDbType = SqlDbType.Int,
        },
        new SqlParameter
        {
            ParameterName = "ID_CLIENT",
            Value = detalles.IdClient,
            SqlDbType = SqlDbType.Int,
            IsNullable=true
        },
    };
    
    return await Task.Run(async () =>
    {
        var result = await _context.Database.SqlQuery<DetailsClientsDto>("EXEC [dbo].[SPROC_DETAILS] @ID_USER", sqlParameters);

        return result;
    }); 
}

public class DetailsClientsDto
{
    public int IdUser { get; set; }
    public int IdClient { get; set; }
    public string User { get; set; }
    public string Adress { get; set; }
    public string Car { get; set; }
    public string Color { get; set; }
}

Prosím, můžete mi říct, jak mám provést postup a vrátit data, děkuji.

1

Nejlepší odpověď

0

Toto je příklad:

// Load this namespace to use SqlParameter
using Microsoft.Data.SqlClient;

//.. or use db.Set<DetailsClientsDto>()
var result = await db.DetailsClientsDto 
  .FromSqlRaw("EXEC [dbo].[SPROC_DETAILS] @idUser, @anotherParam", 
    new SqlParameter("idUser", value1),     
    new SqlParameter("anotherParam", value2))
  .AsNoTracking()
  .ToListAsync()
2021-11-24 01:46:45

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................