Hello guys,
I’ve got a strange problem when deleting an entity in ASP.NET MVC using Ormilite…
here’s the code I use to delete the entity
public void Destroy<T>(T entity)
{
using (var db = dbFactory.Open())
{
db.Delete<T>(entity);
}
}
and what it generates this statement
exec sp_executesql
N’DELETE FROM “NMC_FT_TEST_DEVELOPMENT” WHERE “ID”=@ID AND “DESCRIZIONE”=@DESCRIZIONE AND “TIMESTAMP”=@TIMESTAMP AND “DATA_ESECUZIONE”=@DATA_ESECUZIONE AND “PREZZO”=@PREZZO AND “QUANTITA”=@QUANTITA’,
N’@ID int,@DESCRIZIONE nvarchar(6),@TIMESTAMP datetime,@DATA_ESECUZIONE datetime,@PREZZO decimal(2,0),@QUANTITA int’,
@ID=4,@DESCRIZIONE=N’SECOND’,@TIMESTAMP=NULL,@DATA_ESECUZIONE=‘2015-01-06 00:00:00’,@PREZZO=15,@QUANTITA=NULL
The problem is that it puts = instead of is when checking on NULL… what’s wrong?
How can I delete based on primarykey in a generic way so it doesn’t checks on all the fields?
Thanks
Just answered this on StackOverflow:
http://stackoverflow.com/a/27970145/85785
You can use db.DeleteNonDefaults(entity)
or db.DeleteById<T>(entity.GetId())
You can get the Id value with the T.GetId() extension method
FYI, I’ve just made a refactor to how db.Delete
works so NULL fields are moved out of the parameterized query and appended on the SQL clause. So this should now work with v4.0.37+ that’s now available on MyGet:
https://github.com/ServiceStack/ServiceStack.OrmLite/commit/2670b3c11b994df93b72d57c58681168281b1aad