schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

Cannot use Includes on DeleteQueryProvider #633

Open RamonMeffert opened 3 years ago

RamonMeffert commented 3 years ago

Hi! I've been using NPoco for quite a while, and generally I'm very happy. However, I've been trying to work with tables with foreign keys, and I've been having some issues. The most important one is that I cannot use db.DeleteMany().Where() with foreign keys in the where statement.

The code I am trying to use looks something like this:

[TableName("Example")]
public class Example {
    [Reference(ReferenceType.Foreign, ColumnName = "ex_foreignId", ReferenceMemberName = "Id" ]
    public Foreign Foreign { get; set; }

    public void DeleteManyThings() {
        Database db = GetDatabase();
        db.DeleteMany<Example>()
          .Where(ex => ex.Foreign.Id == 3)
          .Execute();
    }
}

Where Foreign is defined as follows:

[TableName("Foreign")]
public class Foreign {
    [Column("f_Id"]
    public int Id { get; set; }
}

This doesn't work, and throws an error saying that the f_Id column does not exist on my table (which makes sense, since it should be looking for the ex_foreignId key in this example).

Ideally, what I would like to do, is the following:

public void DeleteManyThings() {
    Database db = GetDatabase();
    db.DeleteMany<Example>()
      .Include(ex => ex.Foreign)
      .Where(ex => ex.Foreign.Id == 3)
      .Execute()
}

Basically, the same as when querying a database with foreign keys.

Right now, I'm solving my problem by using ReferenceType.OneToOne and declaring the columns I want to match on manually, but that feels a bit wrong.

I'm using NPoco 5 with SQL Server. Thanks in advance!

PS Thank for making NPoco, it has definitely improved the way me and my colleagues work with our database :)

schotime commented 3 years ago

Unfortunately the OneToOne is the way to go. The include was only ever designed for simple scenarios of which a delete from is not.