Pregunta

OK, estoy bastante seguro de que es solo una cuestión de aprendizaje ... pero tengo un DB muy normalizado con el que estoy trabajando, así que cuando guardo a mi producto tbl, también tengo un productdollar tblle y así sucesivamente.. Mi pregunta está en Silverlight, todo es Async, así que ¿cómo guardo un producto recuperar su nuevo ID y use eso como el producto productDollar.productid FK

hasta ahora con mis otros salvados, solo uso la remonta en la devolución de llamada de los sumitchanges Y ahí, verifico, lo revise y hago la próxima guardada y así sucesivamente ... y encadenándolos así.

Pero tengo 500 productos que necesito para ahorrar (todos a la vez) así que hacer un folleto alrededor de mi objeto de producto no funcionará debido a la maravillosa Async Entonces, ¿qué me estoy perdiendo?Cualquier ayuda o punteros sería muy apreciada

¿Fue útil?

Solución

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top