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

Custom Data Filter on ABP MVC #6507


User avatar
0
andmattia created

about this article Articles/How To/add custom data filter ef core is possibile to do the same on MVC abp (Abp version 4.2)?


3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Do you mean MVC 5, which uses EF 6?

    https://aspnetboilerplate.com/Pages/Documents/Data-Filters#entity-framework

  • User Avatar
    0
    andmattia created

    Thank @aaron to your suggestion but after a lot of time this approch not work. Here is my code

    public interface IMustHaveCompany
        {
            long CompanyId { get; set; }
        }
        
        
        public class Person : FullAuditedEntity, IMustHaveCompany
        {
            public long CompanyId { get; set; }
            public string Name { get; set; }
        }
    
    
    // On coreModule -> PreInitialize()
    ...
    Configuration.UnitOfWork.RegisterFilter("CompanyFilter", true);
    
    // On DbContext Class
    
    public override void Initialize()
            {
                base.Initialize();
                // If I uncomment this the stack say me 
                // System.ApplicationException: Filter name CompanyFilter not found
                //this.SetFilterScopedParameterValue(
                //    "CompanyFilter",
                //    "companyId",
                //    2);
            }
    
            protected void OnodelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Filter("CompanyFilter",
                    (IMustHaveCompany entity, int companyId) => entity.CompanyId == companyId, () =>
                    {
                        return 19;
                    });
            }
            
          // I try to call on TenantDashboardAppService
           CurrentUnitOfWork.SetFilterParameter("CompanyFilter", "CompanyId", 19);
                var q = _personManager.Persons.ToArray();
                
       public interface IPersonManager : IDomainService
        {
            IQueryable<Person> Persons { get; }
        }
        
        public class PersonManager : demoServiceBase, IPersonManager
        {
            private readonly IRepository<Person> _repository;
    
            public PersonManager(IRepository<Person> repository)
            {
                _repository = repository;
            }
    
            public virtual IQueryable<Person> Persons
            {
                get { return _repository.GetAll(); }
            }
        }
    

    What is wrong??? When I look on Configuration.UnitOfWork the filter is regitered on PreInit with 0 parameter, but it's the same for tenant May/Must

  • User Avatar
    0
    aaron created
    Support Team