views:

48

answers:

0

I have a web app that sends ADO Entity data model classes over a WCF service to Silverlight.

Imagine this simplified structure for the database

[Item] Id Name

[ItemDetail] Id ItemId value

While the Item is in my SilverlightApp I add an ItemDetail to the Item, this is ADO so it's something like:

var fv = new ItemDetail();
            fv.Value = value;
            fv.ItemId = Item.Id;
            Item.ItemDetails.Add(fv);

The problem is with the following code when I push this object back over my service it saves only the changes to existing records, not the new ones I've created.

foreach (var item in entities)
        {
            if(item.Id == 0)
            {
                cEnts.Items.AddObject(item);
            }
            else
            {
                EntityKey key = cEnts.CreateEntityKey("Items", item);
                object orig;
                if(cEnts.TryGetObjectByKey(key,out orig))
                {
                    cEnts.ApplyCurrentValues(key.EntitySetName,item);
                }
            }
        }

        cEnts.SaveChanges();

There has to be a better way than going through everything and manually checking then attaching to the object graph. . . that would mean flimsy code which has to be updated each time the database is altered and remove the whole point of an ORM.

Is there a method I'm missing that goes through the object in question and attaches anything new from a detached object?

I don't even think the checking it it's new should be there, shouldn't there be a function that says "Ok, here's a detached object, figure out where it belongs (possibly based on the key) and add it and any children that live in other tables to the database"

I'm not sure if that's the right phrase, but I think I've explained what I want to do.