Save() using actual type instead of declared type

Given a class A with a subclass ASub.
I’ve got an object of type ASub.

public MySave(A obj) {
  db.Save(obj);   // want to save to A table, not ASub table
}

This will try to save to ASub table instead of A table.
How can I force it to use the A table? I will never use ASub table. Ever.

I have a workaround:

public MySave(A obj) {  // obj passed in is actually of type ASub 
  A forcedObj = new().PopulateWith(obj);
  db.Save(forcedObj);
  obj.Id = forcedObj.Id;
}

This forces the save to go to A table no matter which subclass of A is actually passed in.

1 Like