0

好吧,我敢肯定它只是一個學習的事情......但我有一個非常標準化的分貝我跟這樣的工作,當我保存到我的產品TBL我也有一個productDollar tble等... 我的問題是在silverlight一切都是異步如此我如何保存產品取回它的新身份證,並使用它作爲productDollar.productID fkSilverlight 4的WCF RIA保存多個記錄

到目前爲止與我的其他保存我只是在提交變更 的回調中使用submitOperation,然後在那裏檢查是否已完成,然後執行下一個保存等操作,然後將它們鏈接在一起。

但我有500個產品我需要保存(全部一次) 因此,做我的產品對象的foreach不會工作,因爲美妙的異步 所以我錯過了什麼?任何幫助或指針將不勝感激

回答

3

WCF RIA服務出現過這種情況考慮當它被創建。您可以輕鬆地在一個SubmitChanges請求和一個數據庫事務中完成所有操作(取決於您的數據庫和/或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(); 

這導致發送到DomainService一個請求。然而,WCF RIA分裂這個ChangeSet含3插入到3個電話給你DomainService方法:

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

如果您DomainService執行在一個事務中的所有刀片,產品ID可以由您的ORM正確管理。

+0

ohh今天早上你在哪裏!我知道它必須更簡單,然後我就是這麼做的,你的例子很適合我在做什麼......我只是使用ef4開箱即用,通過視覺工作室創建我的domainservice一個嚮導,在添加後彈出那種類型的。真棒謝謝你SOOOOOO很多! – Josh 2011-05-18 04:15:12