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
方法:
InsertProduct(Product product)
InsertProductDollar(ProductDollar productDollar)
InsertProductDollar(ProductDollar productDollar)
如果您DomainService
執行在一個事務中的所有刀片,產品ID可以由您的ORM正確管理。
ohh今天早上你在哪裏!我知道它必須更簡單,然後我就是這麼做的,你的例子很適合我在做什麼......我只是使用ef4開箱即用,通過視覺工作室創建我的domainservice一個嚮導,在添加後彈出那種類型的。真棒謝謝你SOOOOOO很多! – Josh 2011-05-18 04:15:12