Pergunta

Ok, tenho certeza que é apenas uma questão de aprender ... mas eu tenho um banco de dados muito normalizado Eu estou trabalhando assim quando eu economizo no meu produto TBL Eu também tenho um producollar tble e assim por diante.. Minha pergunta é em silverlight tudo é assíncrono, então como eu salvo um produto retomar seu novo ID e usar isso como o producollar.productid fk

Até agora, com os meus outros economos apenas usamos a apresentação de repartição no retorno de chamada das submitchanges e lá, eu verifico para ser conectado e fazer o próximo salvo e assim por diante ... e encadear-os juntos assim.

Mas eu tenho 500 produtos que eu preciso salvar (tudo de uma vez) Então, fazer uma foreach em torno do meu objeto de produto não funciona por causa do maravilhoso Async Então, o que estou perdendo ???Qualquer ajuda ou ponteiros seria muito apreciada

Foi útil?

Solução

WCF RIA Services had this situation in mind when it was created. You can easily do it all in one SubmitChanges request and in one database transaction (depending on your DB and/or ORM). However, if you provide some more information about your objects (POCO, EF, etc.), you'll get a better answer.

That said, I'll take a wild guess at your objects as defined on the server.

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; }
}

And your DomainService looks something like

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
}

Now, on your client, you'll have code that creates and adds these entities.

var context = new ProductDomainContext();

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

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

context.SubmitChanges();

This results in one request sent to the DomainService. However, WCF RIA splits this ChangeSet containing the 3 inserts into 3 calls to your DomainService methods:

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

If your DomainService performs all inserts in one transaction, the ProductID can be correctly managed by your ORM.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top