Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework, WCF & updates

I have created an n-tier solution where I am retrieving related data from a WCF service, updating it within a Windows Forms application, and then returning the updated data via WCF to be persisted to the database. The Application, WCF Service and Database are all on different machines.

The data being retrieved consists of an object and child objects...

public Product Select(string catalogueNumber) {

  return (from p in this.ProductEntities.Products.Include(@"Tracks")
            where p.vcCatalogueNumber == catalogueNumber
            select p).FirstOrDefault() ?? new Product();
}

The updates being applied by the client application can, as well as updating existing content, also insert additional "Track" objects.

When I receive the Product object back from the client application, I can see all of the updates correctly, however in order to save all of the changes correctly I have to jump through a few hoops...

public void Save(Product product) {

    Product original = this.Select(product.vcCatalogueNumber);
    if (original.EntityKey != null) {

        this.ProductEntities.ApplyPropertyChanges(product.EntityKey.EntitySetName, product);

        // There must be a better way to sort out the child objects...
        foreach (Track track in product.Tracks.ToList()) {

            if (track.EntityKey == null) {
                original.Tracks.Add(track);
            }
            else {
                this.ProductEntities.ApplyPropertyChanges(track.EntityKey.EntitySetName, track);
            }

        }

    }
    else {

        this.ProductEntities.AddToProducts(product);

    }

    this.ProductEntities.SaveChanges();

}

Surely, there has to be an easier way to do this?

Note: I have spent the better part of the afternoon investigating the EntityBag project, but found that this has not been updated to work with EF RTM. In particular, whilst it will successfully update the existing data exceptions are thrown when mixing in new objects.

like image 851
Martin Robins Avatar asked Apr 29 '09 19:04

Martin Robins


1 Answers

I don't have a ready-made answer for your particular scenario - but just a question: have you checked out ADO.NET Data Services (f.k.a. "Astoria") ?

They're built on top of Entity Framework, WCF's RESTful interface, and they offer a client-side experience, plus they also seem to have a decent story for not just querying, but also updating, inserting records into databases.

Could this be an option?

Check them out on MSDN, at David Hayden's blog, on Channel9, or see some of the excellent sessions at MIX08 and MIX 09

Marc

like image 90
marc_s Avatar answered Oct 06 '22 22:10

marc_s