0

わかりました、それは単なる学習の問題だと確信しています...しかし、私が使用している非常に正規化されたデータベースがあるので、製品tblに保存すると、productDollarテーブルなどもあります...私の質問Silverlight ではすべてが非同期なので、製品を保存して新しい ID を取得し、それを productDollar.productID fk として使用するにはどうすればよいですか

これまでのところ、他の保存では、submitchanges のコールバックで submitOperation を使用するだけで、そこで iscompleted をチェックし、次の保存などを行います...そして、そのようにそれらを連鎖させます。

しかし、保存する必要がある製品が 500 個あります (一度に) ので、製品オブジェクトの周りで foreach を実行しても機能しません。どんな助けや指針も大歓迎です

4

1 に答える 1

3

WCF RIA Services は、作成時にこの状況を念頭に置いていました。1 つのリクエストと 1 つのデータベース トランザクションですべてを簡単に実行できSubmitChangesます (DB や ORM によって異なります)。ただし、オブジェクト (POCO、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();

これにより、1 つの要求が に送信されDomainServiceます。ただし、WCF RIAChangeSetは、3 つの挿入を含むこれをDomainServiceメソッドへの 3 つの呼び出しに分割します。

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

1 回のトランザクションですべての挿入をDomainService実行すると、ORM で ProductID を正しく管理できます。

于 2011-05-18T02:04:12.880 に答える