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 :)
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:
Where
Foreign
is defined as follows: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 theex_foreignId
key in this example).Ideally, what I would like to do, is the following:
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 :)