public void CloneObject() { IDocumentRepository docRep = new DocumentRepository(); IProductRepository prodRep = new ProductRepository(); ICostDetailRepository cdetRep = new CostDetailRepository(); PapiroService p = new PapiroService(); p.DocumentRepository = docRep; p.CostDetailRepository = cdetRep; p.DocumentRepository.SetDbName("castello"); p.CostDetailRepository.SetDbName("castello"); Document doc = docRep.GetSingle("00000J"); var prod = docRep.GetDocumentProductsByCodProduct(doc.DocumentProducts.First().CodProduct).FirstOrDefault(); DocumentProduct prod2 = (DocumentProduct)prod.Clone(); prod2.CodDocumentProduct = ""; prod2.Document = null; doc.DocumentProducts.Add(prod2); doc.DocumentProductsCodeRigen(true); p.DocumentRepository.Edit(doc); p.DocumentRepository.Save(); p.DocumentRepository.SetDbName("castello"); //array di sostituzione dei codici Dictionary<string, string> trans = new Dictionary<string, string>(); foreach (var c in prod.Costs) { var y = p.CostDetailRepository.GetSingleSimple(c.CodCost); if (y != null) { var x = (CostDetail)y.Clone(); x.CodCostDetail = x.CodCostDetail.Replace(prod.CodDocumentProduct, prod2.CodDocumentProduct); x.CodCost = x.CodCostDetail; x.CostDetailCostCodeRigen(); if (x.CodComputedBy != null) { x.CodComputedBy = x.CodComputedBy.Replace(prod.CodDocumentProduct, prod2.CodDocumentProduct); } p.CostDetailRepository.Add(x); } } p.CostDetailRepository.Save(); doc = docRep.GetSingle("00000J"); Console.WriteLine(""); }