question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Filter not working in EF 6

See original GitHub issue

I try to implement a custom filter in my multi dbcontext and not work correctly, I try to implement in a fresh solution but I have the same error (ABP version 4.2 / MVC / AngularJs) 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:26 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ismcagdascommented, Feb 19, 2019

No problem, I will try to reproduce it. It should be easy.

0reactions
zfmycommented, Jun 13, 2019

Thank you for your attention. I may figured it out.the GetCurrentUsersOuIdOrNull only takes the value from claims. It should check CurrentUnitOfWorkProvider.Current.Filters firstly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF 6 - can not use string in filter when querying
Well, it seems the answer to this issue is using ObjectQuery from ObjectContext. However, this seems a little confusing to me.
Read more >
Filters in ASP.NET Core
Learn how filters work and how to use them in ASP.NET Core.
Read more >
How To Add Custom Data Filter in EF Core
In this article, I will explain how to add a custom data filter in Entity Framework Core. We will create a filter for...
Read more >
Tutorial: Add sorting, filtering, and paging with the Entity ...
In this tutorial you add sorting, filtering, and paging functionality to the **Students** Index page. You also create a simple grouping ...
Read more >
How to filter entities not contained from an existing list with ...
Filtering entities by excluding all items from an existing list can sometimes be very useful. For example, you deserialize a JSON file into ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found