Ido
Ido

Reputation: 188

Allow IgnoreQueryFilters followed by select query

I am using ef core V2.1 with soft-delete.

I tried to fetch a student, with all his milestones record (include, some which were deleted), using the IgnoreQueryFilters() method.

But while I could do this:

applicationDbContex.Students
.IgnoreQueryFilters().Include("Milestones.Type")
.Where(...)
.ToArray();

When I tried to add a select statment after the where clause, It did not fetch the deleted milestones.

applicationDbContex.Students
     .IgnoreQueryFilters().Include("Milestones.Type").ToArray();         
     .Where(...)
     .Select(s => new {
         PersonalNumber = s.PersonalNumber,
         Milestones = s.Milestones 
     })

Here is the simplified models i am using:

public class Student {

    public ICollection<Milestone > Milestones {get; set;}
}

public class Milestone {
    ..
    public MilestoneType type {get; set;}
    ..
}

public class MileStoneType {
    ..
    public int Id {get; set;}
    ..
}

Any suggestions of what might be the problem?

EDIT:

I wish to state, that I use 'UseLazyLoadingProxies()' in startup.cs

 services.AddDbContext<ApplicationDbContext>(options => 
                options.UseLazyLoadingProxies()
                    .UseSqlServer(connectionString)
            );

It seems, that inorder to select query to work, I have to fetch a complete object , for instance the code works only, if I use the following code before:

 applicationDbContext.Soldiers.IgnoreQueryFilters()
            .Include("Milestones.Type").Load();

Upvotes: 1

Views: 5863

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

you must call ignoreQueryFilter befor where and select

applicationDbContex.Students.IgnoreQueryFilters().Where(...)
     .Select(s => new {
         PersonalNumber = s.PersonalNumber,
         Milestones = s.Milestones 
     })
     .Include("Milestones.Type").ToArray();

and Include is useless when you use Select!

Upvotes: 1

Related Questions