Base solution for your next web application
Open Closed

Stored Procedure Not working - Asp.net Core #3345


User avatar
1
thobiasxp created

Hi, Good Evening,

We have custom dbcontext and we try to run store procedure while on development that Store procedure is running perfectly but when we publish and try to run stored procedure its showing as internal error. Kindly provide us solution.

////Db Context

public class SpDbContext : DbContext
    {
        public SpDbContext()
            : base(GetConnectionString())
        {

        }

        private static string GetConnectionString()
        {
            //Notice that; this logic only works on development time.
            //It is used to get connection string from appsettings.json in the Web project.

            var configuration = AppConfigurations.Get(
                WebContentDirectoryFinder.CalculateContentRootFolder()
                );

            return configuration.GetConnectionString(
                stemConsts.ConnectionStringName
                );
        }

        public SpDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }

        public SpDbContext(DbConnection existingConnection)
            : base(existingConnection, false)
        {

        }

        public SpDbContext(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {

        }
    }


// Method of Stored Procedure


public async Task DeleteCity(EntityDto input)
        {
             try
                {
                using (var context = new SpDbContext())
                  {

                    var idd = new SqlParameter
                    {
                        ParameterName = "TableId",
                        Value = 2
                    };
                    var list = context.Database.SqlQuery<FindDelete>("exec Sp_FindMappedTable @TableId", idd).ToList();

                    var count = list.Where(f => f.id == input.Id).ToList();
                    if (count.Count() <= 0)
                    {

                        await _cityRepository.DeleteAsync(input.Id);
                    }
                    else
                    {
                        throw new UserFriendlyException("Unable to Delete", "Data is being used by another Field");
                    }
                }
                }
                catch (Exception ex)
                {

                }
        }

Thanks


7 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    Have you looked at the logs(Logs.txt and browser console error messages)?

  • User Avatar
    0
    thobiasxp created

    Hi, Good Morning,

    This is the error message we are getting in log.

    DEBUG 2017-06-08 10:32:17,427 [17   ] osoft.AspNetCore.Routing.Tree.TreeRouter - Request successfully matched the route with name '(null)' and template 'api/services/app/Country/DeleteCountry'.
    DEBUG 2017-06-08 10:32:17,431 [17   ] ore.Mvc.Internal.ControllerActionInvoker - Executing action tibs.stem.Countrys.CountryAppService.DeleteCountry (tibs.stem.Application)
    INFO  2017-06-08 10:32:17,466 [17   ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method tibs.stem.Countrys.CountryAppService.DeleteCountry (tibs.stem.Application) with arguments (Abp.Application.Services.Dto.EntityDto) - ModelState is Valid
    ERROR 2017-06-08 10:32:17,483 [17   ] Mvc.ExceptionHandling.AbpExceptionFilter - Could not find content root folder!
    System.ApplicationException: Could not find content root folder!
       at tibs.stem.Web.WebContentDirectoryFinder.CalculateContentRootFolder() in F:\Project Backups\Bafco Backups\bafco 7\aspnet-core\src\tibs.stem.Core\Web\WebContentFolderHelper.cs:line 26
       at tibs.stem.Tenants.Dashboard.SpDbContext.GetConnectionString() in F:\Project Backups\Bafco Backups\bafco 7\aspnet-core\src\tibs.stem.Application\Tenants\Dashboard\SpDbContext.cs:line 27
    

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    This error is throwed by this class <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/master/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Core/Web/WebContentFolderHelper.cs">https://github.com/aspnetzero/aspnet-ze ... rHelper.cs</a> and normally it is only used in unit tests and entity framework core command line commands.

    I assume you have used it somehow. Can you share your custom repository code for running SP ?

    Thanks.

  • User Avatar
    0
    thobiasxp created

    Hi, Good Morning,

    We don't have customer custom repository. we are trying to access database from this code of yours

    public class SpDbContext : DbContext
        {
            public SpDbContext()
                : base(GetConnectionString())
            {
    
            }
    
            private static string GetConnectionString()
            {
                //Notice that; this logic only works on development time.
                //It is used to get connection string from appsettings.json in the Web project.
    
                var configuration = AppConfigurations.Get(
                    WebContentDirectoryFinder.CalculateContentRootFolder()
                    );
    
                return configuration.GetConnectionString(
                    stemConsts.ConnectionStringName
                    );
            }
    
            public SpDbContext(string nameOrConnectionString)
                : base(nameOrConnectionString)
            {
    
            }
    
            public SpDbContext(DbConnection existingConnection)
                : base(existingConnection, false)
            {
    
            }
    
            public SpDbContext(DbConnection existingConnection, bool contextOwnsConnection)
                : base(existingConnection, contextOwnsConnection)
            {
    
            }
        }
    

    and we are calling above class inside our method like this

    // Method

    public async Task DeleteCity(EntityDto input) { try { using (var context = new SpDbContext()) {

                    var idd = new SqlParameter
                    {
                        ParameterName = "TableId",
                        Value = 2
                    };
                    var list = context.Database.SqlQuery&lt;FindDelete&gt;("exec Sp_FindMappedTable @TableId", idd).ToList();
    
                    var count = list.Where(f => f.id == input.Id).ToList();
                    if (count.Count() <= 0)
                    {
    
                        await _cityRepository.DeleteAsync(input.Id);
                    }
                    else
                    {
                        throw new UserFriendlyException("Unable to Delete", "Data is being used by another Field");
                    }
                }
                }
                catch (Exception ex)
                {
    
                }
        }
    

    This code is working perfectly fine when we run from the visual studio but when we publish and host it on server we are getting above errors. can you help us sort it out... otherwise is there any other way to call stored procedure.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you move it into a custom repository and try again ?

    When you instantiate the dbContext (var context = new SpDbContext()), it's parameterless constructor is called and it causes the problem. As metioned in the code, GetConnectionString()method only works for development time.

    Thanks.

  • User Avatar
    0
    thobiasxp created

    Hi, Good Morning,

    We have created customer repository by adding reference from core and code is below. If its wrong can u give us sample custom repository code.

    public class CountryAppService : stemAppServiceBase, ICountryAppService
        {
            private readonly stemDbContext _spDbContext;
    
            public CountryAppService(stemDbContext spDbContext)
            {
                _spDbContext = spDbContext;
            };
    
    
            public async Task GetDeleteCountry(EntityDto input) 
            {
    
                    var idd = new SqlParameter
                    {
                        ParameterName = "TableId",
                        Value = 1
                    };
    
                    var list = _spDbContext.Database.SqlQuery<FindDelete>("exec Sp_FindMappedTable @TableId", idd).ToList();
    
                    var count = list.Where(f => f.id == input.Id).ToList();
                    if (count.Count() <= 0)
                    {
    
                        await _countryRepository.DeleteAsync(input.Id);
                    }
                    else
                    {
                        throw new UserFriendlyException("Unable to Delete", "Data is being used by another Field");
                    }
    
     }       
    }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It seems like you have created an app service. This is a custom repository for EF Core <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.EntityFrameworkCore/MultiTenancy/Payments/SubscriptionPaymentRepository.cs">https://github.com/aspnetzero/aspnet-ze ... ository.cs</a>.

    It will be very similar for EF as well.

    Thanks.