实体框架,WCF和更新

Mar*_*ins 10 wcf entity-framework n-tier-architecture

我创建了一个n层解决方案,我从WCF服务检索相关数据,在Windows窗体应用程序中更新它,然后通过WCF返回更新的数据以保存到数据库.应用程序,WCF服务和数据库都在不同的机器上.

检索的数据包含一个对象和子对象......

public Product Select(string catalogueNumber) {

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

客户端应用程序应用的更新可以以及更新现有内容,还可以插入其他"跟踪"对象.

当我从客户端应用程序收到Product对象时,我可以正确地看到所有更新,但为了正确保存所有更改,我必须跳过几个箍......

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();

}
Run Code Online (Sandbox Code Playgroud)

当然,必须有一个更简单的方法来做到这一点?

注意:我花了大部分时间调查EntityBag项目,但发现它尚未更新以使用EF RTM.特别是,虽然它将成功更新现有数据,但在混合新对象时会抛出异常.

mar*_*c_s 3

对于您的特定情况,我没有现成的答案 - 只是一个问题:您检查过 ADO.NET 数据服务(fka“Astoria”)吗?

它们构建在实体框架、WCF 的 RESTful 接口之上,提供客户端体验,此外,它们似乎也有一个不错的故事,不仅可以查询,还可以更新、将记录插入数据库。

这可以是一个选择吗?

在MSDNDavid Hayden 的博客Channel9上查看,或者查看MIX08 和 MIX 09上的一些精彩会议

马克