Giter Club home page Giter Club logo

Comments (19)

jcachat avatar jcachat commented on June 2, 2024

What happens when you call "context.Instructors.ToList()"? Is it not filtering on IsDeleted or throwing an error?

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

I set up a test using the models and filter exactly as you posted here. Everything is working correctly for me. The filter is applied when I query from Students, Instructors, and People. I'm gong to need some more information from you about what exactly is the problem you are seeing as you didn't state what the problem is, just that you are having one... :)

from entityframework.dynamicfilters.

TefoT4 avatar TefoT4 commented on June 2, 2024

I added two instructor records to the Db then marked the last record as deleted.
It threw and exception. Property "IsDeleted" not found on EdmType Instructor.

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

I just pushed up the test I created to try to duplicate this. Can you take a look at that and compare it to what you are doing? We must be doing something different since it works for me... Or if you can prepare a full solution to demonstrate it and email it to me ([email protected]), that would work too.

from entityframework.dynamicfilters.

TefoT4 avatar TefoT4 commented on June 2, 2024

Let me prep up a solution. I will email it soon as i'm done. :)

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

I am also having the same problem, did you ever find a solution to this?

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

No, I am not able to reproduce the problem with the models TefoT4 posted and still waiting on a full solution that demonstrates the problem. If you have more information on how to reproduce it, please post it and I'll take a look.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

My setup is slightly different but should work the same. I get the error found in LambdaToDbExpressionVisitor line 248 formatted like this: Property Deleted not found in Entity Type BaseModel

Now, that is strange to me because I do not see my setup as accessing entities outside of the main model (if this is the case would you be happy for me to pull the code and implement that functionality)?

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Filter("SoftDelete", (BaseModel x) => x.Deleted == null);
        base.OnModelCreating(modelBuilder);
    }
    public class BaseModel
    {
        public DateTime? Created { get; set; }

        [MaxLength(32)]
        public String CreatedBy { get; set; }

        public DateTime? Updated { get; set; }

        [MaxLength(32)]
        public String UpdatedBy { get; set; }

        [Index]
        public DateTime? Deleted { get; set; }
    }
    public class Person : BaseModel
    {
        public int Id { get; set; }

        [Required, Display(Name = "First Name"), MaxLength(32)]
        public string FirstName { get; set; }

        [Display(Name = "Middle Name"), MaxLength(64)]
        public string MiddleName { get; set; }

        [Required, Display(Name = "Last Name"), MaxLength(32)]
        public string LastName { get; set
    }

    [Table("Residents")]
    public class Resident : Person
    {
        [Required, MaxLength(4)]
        public string Title { get; set; }

        [MaxLength(32), Display(Name = "Maiden Name")]
        public string MaidenName { get; set; }

        [MaxLength(32), Display(Name = "Famailiar Name")]
        public string FamiliarName { get; set; }

        [Required, MaxLength(1)]
        public string Gender { get; set; } // M or F

        [Display(Name = "Date of Birth"), DataType(DataType.Date)]
        public DateTime? DateOfBirth { get; set; }
    }

I get the above error whenever I try to query anything, Residents, People or any other entity - so on reflection this might not be directly related to the TPT setup and more about the use of BaseModel as a class rather than an interface, either way it feels like what I am doing should work/should be supported?.

I wrote this up pretty quickly before work, I will happily spend some time to help you reproduce the error I get if this doesn't help later today.

Thanks,

Tom.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

After further investigation the problem seems to be that the 'Resident' entity doesn't have the properties that 'BaseModel' has because it is inheriting them from 'Person'. 'Person' does have the properties from 'BaseModel'. So when the code tries to find the 'Deleted' property on 'Resident' it just falls over and dies.

This still seems strange because you can obviously access the 'Deleted' property in the linq queries.

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

I tried reproducing this using the models and filter exactly as you posted and it works fine for me... What database and which version of EF are you using (I tried with 6.1.2 and 6.1.3)? I may need you to package up a full solution and email it to me ([email protected]) so I can figure out what's different.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

Ef 6.1.3 & sql server

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

Could you send me what you did to reproduce ([email protected])?

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

The problem seems to be caused by you having a DbSet created in your DbContext in addition to the DbSet. If you remove that, it works fine. I'm not sure yet why that causes a problem but I'll dig into it and figure it out. It might be a couple days though - I'm actually on vacation right now and traveling tomorrow.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

"DbSet created in your DbContext in addition to the DbSet" - Sorry, what do you mean? Don't I have to have my DbSet's defined in my DbContext?

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

In your ApplicationDbContext class, you have these 2 lines:

public DbSet<Person> People { get; set; }
public DbSet<Resident> Residents { get; set; }

At least in the project you sent me, "People" is not referenced so you can safely remove it. But, that obviously changes your table structure and will result in a single Residents table instead of 2 tables which is probably not what you're going for.

Just a theory/guess, but... I think the use of TPT between People and Residents and the inheritance (non-TPT) between People and BaseModel is what is causing the issue. But no idea if it's something inside EF or with my filters. If you want to work around this (for now at least), you could move everything from BaseModel into People, create an IBaseModel interface for those fields, and then create the filter against that interface.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

That is exactly what I thought too... i'll give that work around you suggest for now, i'll let you know how it goes.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

I am still getting the same problem when I create IBaseModel and add the implementation to Person.

EDIT: could you send me what you did to test it? I can't get mine to work.

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 2, 2024

Found the problem. Fix is pushed and NuGet has been updated.

The problem was being caused by TPT inheritance. Basically, when you query out "Residents", EF is automatically including a join against Person. Since both of those C# classes inherit the deleted filter, it was being applied by each of those 2 DbScanExpressions. And where the Residents C# class has the Deleted property, the Residents database table does not - hence the error. So the fix was to detect this and not apply the filter where it should not be.

Another note (and this may apply more to @TefoT4): Because of how the queries are parsed, the problem is only hit if you create a filter like this:

modelBuilder.Filter("IsDeleted", (IEntity e) => e.IsDeleted == false)

as opposed to this:

modelBuilder.Filter("IsDeleted", (IEntity e) => e.IsDeleted, false)

The difference is one uses a parameter and the other does not. When I changed his filter to the non-parameter version, I hit the same problem with his models. I'm wondering if the trouble reproducing that case was because of this.

from entityframework.dynamicfilters.

tomqwertysdsad avatar tomqwertysdsad commented on June 2, 2024

Great! Thank you for fixing this so fast. Hope you had a good holiday.

EDIT: Just tested this, works a perfectly. Thank you again.

from entityframework.dynamicfilters.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.