0

好的,我很确定这只是学习的问题......但是我正在使用一个非常规范化的数据库,所以当我保存到我的产品 tbl 时,我也有一个 productDollar tble 等等......我的问题在silverlight中一切都是异步的所以我如何保存产品取回它的新ID并将其用作productDollar.productID fk

到目前为止,我的其他保存我只是在 submitchanges 的回调中使用 submitOperation 并在那里我检查 iscompleted 并进行下一次保存等等......然后像这样将它们链接在一起。

但是我有 500 个产品需要保存(一次全部保存),所以围绕我的产品对象做一个 foreach 将无法工作,因为美妙的异步所以我错过了什么?任何帮助或指示将不胜感激

4

1 回答 1

3

WCF RIA 服务在创建时就考虑到了这种情况。您可以在一个请求和一个数据库事务中轻松完成所有SubmitChanges操作(取决于您的数据库和/或 ORM)。但是,如果您提供有关您的对象(PO​​CO、EF 等)的更多信息,您将得到更好的答案。

也就是说,我将对服务器上定义的对象进行疯狂的猜测。

public class Product
{
    [Key]
    public int? ProductID { get; set; }

    // ... more properties ...

    [Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = false)]
    [Include]
    [Composition]
    public ICollection<ProductDollar> ProductDollars { get; set; }
}

public class ProductDollar
{
    [Key]
    public int? ProductDollarID { get; set; }

    public int? ProductID { get; set; }

    // ... more properties ...

    [Association("Product-ProductDollars", "ProductID", "ProductID", IsForeignKey = true)]
    [Include]
    public Product Product { get; set; }
}

你的 DomainService 看起来像

public class ProductDomainService : DomainService
{
    public IQueryable<Product> GetProducts()
    {
        // Get data from the DB
    }

    public void InsertProduct(Product product)
    {
        // Insert the Product into the database

        // Depending on how your objects get in the DB, the ProductID will be set
        // and later returned to the client
    }

    public void InsertProductDollar(ProductDollar productDollar)
    {
        // Insert the ProductDollar in the DB
    }

    // Leaving out the Update and Delete methods
}

现在,在您的客户端上,您将拥有创建和添加这些实体的代码。

var context = new ProductDomainContext();

var product = new Product();
context.Products.Add(product);

product.ProductDollars.Add(new ProductDollar());
product.ProductDollars.Add(new ProductDollar());

context.SubmitChanges();

这导致向DomainService. 但是,WCF RIA 将ChangeSet包含 3 个插入的内容拆分为对您的DomainService方法的 3 个调用:

  1. InsertProduct(Product product)
  2. InsertProductDollar(ProductDollar productDollar)
  3. InsertProductDollar(ProductDollar productDollar)

如果您DomainService在一个事务中执行所有插入,则您的 ORM 可以正确管理 ProductID。

于 2011-05-18T02:04:12.880 回答