Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Error with Custom Repository #1878


User avatar
0
amayorquin created

Hi,

Can you help me, please, I created a Custom Repository to call a stored procedure, and the call to the custom repository is working fine, but I'm getting an error when I test calling the API.

This is my code

Interface in Core project

public interface ILayerAppService : IApplicationService
    {
        Task<ListResultDto<LayerListDto>> GetPersonLayers();
    }

Repository in

public class LayerRepository : CorgeeZeroRepositoryBase<Layer>, ILayerRepository
    {
        public LayerRepository(IDbContextProvider<CorgeeZeroDbContext> dbContextProvider)
        : base(dbContextProvider)
        {
        }

        public List<Layer> GetPersonLayers(long personId)
        {
           List<Layer> layers = new List<Layer>();

            using (var db = GetDbContext())
            {
                db.Database.Initialize(force: false);

                var cmd = db.Database.Connection.CreateCommand();
                cmd.CommandText = "[dbo].[GetPersonLayers]";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter { ParameterName = "@PersonId", Value = personId, SqlValue = personId,   SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input });

                try
                {
                    db.Database.Connection.Open();

                    using (var reader = cmd.ExecuteReader())
                    {
                        layers = ((IObjectContextAdapter)db).ObjectContext.Translate<Layer>(reader).ToList();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    db.Database.Connection.Close();
                }
                return layers;
            }
        }
    }

And the AppService:

public class LayerAppService : CorgeeZeroAppServiceBase, ILayerAppService
    {
        private readonly IRepository<Layer> _layerRepository;
        private readonly ILayerRepository _customLayerRepository;

        public LayerAppService(IRepository<Layer> layerRepository, ILayerRepository customLayerRepository)
        {
            _layerRepository = layerRepository;
            _customLayerRepository = customLayerRepository;
        }

       public async Task<ListResultDto<LayerListDto>> GetPersonLayers()
        {
            var currentUser = await this.GetCurrentUserAsync();
            
            var layers = _customLayerRepository.GetPersonLayers(currentUser.Id);

            var mapping = new ListResultDto<LayerListDto>(layers.MapTo<List<LayerListDto>>());

            return mapping;
        }
    }

I'm getting the results correctly in the mapping var , but for some reason the API is returning an error:

"An internal error occurred during your request!"


3 Answer(s)
  • User Avatar
    0
    andmattia created

    can you share the excption?

  • User Avatar
    0
    ismcagdas created
    Support Team

    And can you also share your LayerListDto ? Does it contain any entity ?

  • User Avatar
    0
    amayorquin created

    Thank you very much for your responses. Finally I changed the way I call the stored procedure and works now.

    I changed to this:

    var  layers = Context.Database.SqlQuery<Layer>("GetPersonLayersBySecurityGroups @PersonId",
                    new SqlParameter("@PersonId", personId)).ToList();