【问题标题】:Silverlight 4 wcf ria Saving multiple recordsSilverlight 4 wcf ria 保存多条记录
【发布时间】:2011-05-17 17:25:28
【问题描述】:

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

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

但我需要保存 500 件产品(一次全部保存) 所以在我的产品对象周围做一个 foreach 是行不通的,因为 async 很棒 那我错过了什么???任何帮助或指示将不胜感激

【问题讨论】:

    标签: entity-framework silverlight-4.0 mvvm-light wcf-ria-services submitchanges


    【解决方案1】:

    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 将包含 3 个插入的 ChangeSet 拆分为对您的 DomainService 方法的 3 个调用:

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

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

    【讨论】:

    • 哦,今天早上你在哪里!!我知道它必须比我正在做的更简单,你的例子几乎就是我在做什么......我只是使用开箱即用的 ef4 并通过添加后弹出的 Visual Studios 小向导创建我的域服务那种类型的课。太棒了,谢谢你,太棒了!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多