I have three entities in my data context which inherit IFileData, but I want just a single delete method. How can this be achieved?
The code below give the error The type "IFileData" is not mapped as a Table.
public void ImageDelete<T>(T instance) where T : class, IFileData
{
using (var db = this.CreateDataContext())
{
db.GetTable<T>().DeleteOnSubmit(instance);
db.SubmitChanges();
}
}
The only solution I have so far is to do a switch on the .GetType() which is a bit messy.
Thanks!
Solution:
As Marc mentioned, I used dynamic in the calling method:
ImageDelete((dynamic)(instance as IFileData));
You could build the
Expressionmanually, using the actualT? Untested (since thatDeletedoesn’t exist?), but something like:This then isn’t bound to
IFileData, but toT. It will fail, however, ifIFileData.Keyis implemented via explicit interface implementation – there must be an obviousKeyfield/property onT.Then: