Ok, the short version is that I have a Linq entity from a LinqDataSourceUpdateEventArgs and I need to handle the Update manually because MS was stupid.
I can get the Table object from the data context, which allows me to, well, this:
var newObj = e.NewObject;
var table = FormContext.GetTable(e.NewObject.GetType());
table.Attach(newObj, e.OriginalObject);
if (BuildingObject != null)
BuildingObject(sender, new HeirarchicalBuildObjectEventArgs(newObj));
FormContext.SubmitChanges();
Unfortunately, I get the exception "Cannot add an entity with a key that is already in use."
Of course, the funny part is I get that on FormContext.SubmitChanges(), NOT on table.Attach()... which makes no sense to me, but whatever.
I'm thinking I need to actually get the object from the context, and attach using that instead of e.OriginalObject... OR, as a last resort, I need to get the original object and write a loop which copies the value of every property into the one I get from the data context.
Either way, I need to look up an object by it's primary key without knowing the type of the object. Is there a way to do that?
EDIT: Ok, took a look through .NET Reflector and I'm noticing that, among other things, LinqDataSourceView attaches the OLD data object and then copies all the values into it... but that apparently skips the associations. Going to try attaching the old object and copying values over, I guess...
The really funny part? I wrote a function to copy properties from one entity instance to another a long time ago, and it contains this comment:
//We can't copy associations, and probably shouldn't
Sometimes I wish my comments were more thorough...
EDIT EDIT: Ok, so once again the correct answer is: I asked the wrong question!
The correct code is:
var newObj = e.NewObject;
var table = FormContext.GetTable(e.NewObject.GetType());
if (BuildingObject != null)
BuildingObject(sender, new HeirarchicalBuildObjectEventArgs(newObj));
table.Attach(newObj, e.OriginalObject);
FormContext.SubmitChanges();
e.Cancel = true;
I originally was trying to attach after BuildingObject, but got some other error and moved the attach statement in an effort to correct it. (I think because I was calling the wrong version of Attach. Or maybe I had the arguments reversed...)