public void removeRiga(RigaCarrello rigaDacanc) { EntityState stato = OrmUtil.getEntityState(rigaDacanc, mioDbContext); // Rimuovo l'elemento dalla collezione. // Non so perché, ma essendo una relazione identificante, perché EF NON si preoccua di rimuovere anche da disco la riga da solo ?? // Dovrebbe chiamare la delete sul db, ma non lo fa! ... bool test = carrello.righeCarrello.Remove(rigaDacanc); if (!test) { _giornale.Error("Si è cercato di cancellare una riga dal carrello che non esiste. Probabilmente la riga era già stata eliminata dal carrello in precedenza"); throw new LumenException("La riga non è presente nel carrello"); } // ... per quanto sopra, se l'oggetto era persistente, devo preoccuparmi io di rimuoverlo anche dal dbcontext if (isStatoModifica == true && rigaDacanc.id != Guid.Empty) { if (stato == EntityState.Detached || stato == EntityState.Modified || stato == EntityState.Unchanged) { mioDbContext.RigheCarrelli.Remove(rigaDacanc); } } isCarrelloModificato = true; }
public Carrello Applica(Carrello cin, Promozione _promo, PromoContext contestoDiVendita) { PromoStessaFotoSuFile promo = (PromoStessaFotoSuFile)_promo; bool elargito = false; // Vediamo se esiste una foto con il rispettivo file. foreach (RigaCarrello r in cin.righeCarrello) { if (r.prezzoNettoTotale > 0 && r.discriminator == RigaCarrello.TIPORIGA_STAMPA) { RigaCarrello rigaFile = cin.righeCarrello.SingleOrDefault(r2 => r2.isTipoMasterizzata && r2.fotografia == r.fotografia); if (rigaFile != null) { // trovato il file che corrisponde a questa foto. rigaFile.sconto = rigaFile.prezzoLordoUnitario - promo.prezzoFile; rigaFile.prezzoNettoTotale = GestoreCarrello.calcValoreRiga(rigaFile); elargito = true; _giornale.Debug("Elargita per foto num. " + rigaFile.fotografia.numero); } } } // Aggiungo la promo alla lista di quelle elargite if (elargito && contestoDiVendita.promoApplicate.Contains(promo) == false) { contestoDiVendita.promoApplicate.Add(promo); } return(cin); }
public void spostaRigaCarrello(RigaCarrello rigaCarrello, bool remove) { if (remove) { carrello.righeCarrello.Remove(rigaCarrello); } if (rigaCarrello.isTipoStampa) { ProdottoFile prodottoFile = mioDbContext.ProdottiFile.Single(); rigaCarrello.discriminator = RigaCarrello.TIPORIGA_MASTERIZZATA; rigaCarrello.quantita = 1; rigaCarrello.prodotto = mioDbContext.ProdottiFile.Single(); rigaCarrello.totFogliStampati = 0; rigaCarrello.bordiBianchi = null; rigaCarrello.prezzoLordoUnitario = prodottoFile.prezzo; rigaCarrello.prezzoNettoTotale = rigaCarrello.prezzoLordoUnitario; rigaCarrello.nomeStampante = null; rigaCarrello.sconto = null; } else if (rigaCarrello.isTipoMasterizzata) { //Quando sposto la riga setto di default i bordi bianchi a false rigaCarrello.bordiBianchi = false; rigaCarrello.discriminator = RigaCarrello.TIPORIGA_STAMPA; } else { _giornale.Warn("Errore è stata spostata una riga senza dicriminator"); } aggiungiRiga(rigaCarrello); ricalcolaDocumento(true); }
// TODO forse sarebbe più consono chiamare il clona su tutti i componenti ?? (carrello, righe, provvigioni) public Carrello ClonaCarrello() { Carrello clonato = new Carrello(); clonato.giornata = DateTime.Today; clonato.tempo = DateTime.Now; clonato.intestazione = carrello.intestazione; clonato.note = carrello.note; clonato.prezzoDischetto = carrello.prezzoDischetto; clonato.totaleAPagare = carrello.totaleAPagare; clonato.totMasterizzate = carrello.totMasterizzate; clonato.visibileSelfService = carrello.visibileSelfService; clonato.righeCarrello = new List <RigaCarrello>(); foreach (RigaCarrello r in carrello.righeCarrello) { RigaCarrello r2 = new RigaCarrello(); r2.carrello = clonato; r2.bordiBianchi = r.bordiBianchi; r2.descrizione = r.descrizione; r2.discriminator = r.discriminator; r2.prodotto = r.prodotto; r2.fotografia = r.fotografia; r2.fotografo = r.fotografo; r2.nomeStampante = r.nomeStampante; r2.prezzoLordoUnitario = r.prezzoLordoUnitario; r2.prezzoNettoTotale = r.prezzoNettoTotale; r2.quantita = r.quantita; r2.sconto = r.sconto; clonato.righeCarrello.Add(r2); } return(clonato); }
private Carrello creaNuovoCarrelloPerTest() { var fotografie = UnitOfWorkScope.currentDbContext.Fotografie.Take(2).ToArray(); var formatoCarta = UnitOfWorkScope.currentDbContext.FormatiCarta.First(); gestoreCarrello.creaNuovo(); int i = 0; RigaCarrello riga1 = new RigaCarrello(); riga1.fotografia = fotografie[i]; riga1.quantita = 2; riga1.fotografo = fotografie[i].fotografo; riga1.prodotto = formatoCarta; riga1.discriminator = RigaCarrello.TIPORIGA_STAMPA; riga1.descrizione = "da stampare"; riga1.nomeStampante = Costanti.NomeStampantePdf; gestoreCarrello.aggiungiRiga(riga1); ++i; RigaCarrello riga2 = new RigaCarrello(); riga2.fotografia = fotografie[i]; riga2.quantita = 1; riga2.fotografo = fotografie[i].fotografo; riga2.discriminator = RigaCarrello.TIPORIGA_MASTERIZZATA; riga2.descrizione = "da masterizzare"; gestoreCarrello.aggiungiRiga(riga2); gestoreCarrello.carrello.intestazione = Tag; gestoreCarrello.carrello.prezzoDischetto = 15.5m; gestoreCarrello.salvare(); return(gestoreCarrello.carrello); }
public void rigaCarrelloStaccaFormatoCarta() { FormatoCarta formato; using (LumenEntities dbContext = new LumenEntities()) { formato = dbContext.FormatiCarta.FirstOrDefault(); } Fotografia fotografia; using (LumenEntities dbContext = new LumenEntities()) { fotografia = dbContext.Fotografie.FirstOrDefault(); } Fotografo fotografo; using (LumenEntities dbContext = new LumenEntities()) { fotografo = dbContext.Fotografi.FirstOrDefault(); } using (LumenEntities dbContext = new LumenEntities()) { dbContext.FormatiCarta.Attach(formato); dbContext.Fotografi.Attach(fotografo); dbContext.Fotografie.Attach(fotografia); RigaCarrello rr = new RigaCarrello(); rr.discriminator = RigaCarrello.TIPORIGA_STAMPA; rr.prodotto = formato; rr.fotografo = fotografo; rr.fotografia = fotografia; } }
public void CarrelloTest1() { using (new UnitOfWorkScope()) { Carrello carrelloTest = recuperaCarrelloPerTest(); gestoreCarrello.caricaCarrello(carrelloTest.id); // Cambio una info nella testata e una descrizione in un riga carrelloTest.note = DateTime.Now.ToString(); // Cambio anche una nota sulle righe per causare foreach (var riga in carrello.righeCarrello) { riga.descrizione = DateTime.Now.ToString(); } int numRigheOrig = carrello.righeCarrello.Count; // ora aggiungo una riga in fondo. Prendo una foto caso che non sia già nel carrello var fotos = carrello.righeCarrello.Select(r => r.fotografia).Distinct(); RigaCarrello riga3 = new RigaCarrello(); riga3.fotografia = UnitOfWorkScope.currentDbContext.Fotografie.AsEnumerable().Except(fotos).First(); riga3.quantita = 1; riga3.fotografo = riga3.fotografia.fotografo; riga3.prodotto = UnitOfWorkScope.currentDbContext.FormatiCarta.First(); riga3.discriminator = RigaCarrello.TIPORIGA_STAMPA; riga3.descrizione = "DACANC"; riga3.nomeStampante = Costanti.NomeStampantePdf; gestoreCarrello.aggiungiRiga(riga3); // Prima salvo senza cambiare niente. gestoreCarrello.salvare(); // ----- // Eseguo una query sql con un altra connessione, per vedere che la riga sia stata aggiunta object[] parametri = { carrello.id }; var cntQuery = UnitOfWorkScope.currentDbContext.Database.SqlQuery <int>("select count(*) from RigheCarrelli where carrello_id = {0}", parametri); int numRigheA = cntQuery.First <int>(); Assert.IsTrue(numRigheA == numRigheOrig + 1); // Ora riprendo il carrello ed elimino la riga 3 gestoreCarrello.caricaCarrello(carrelloTest.id); RigaCarrello rigaDacanc = carrello.righeCarrello.Single(r => r.descrizione == "DACANC"); gestoreCarrello.removeRiga(rigaDacanc); // Eseguo una query sql con un altra connessione, per vedere che ancora non c'è stato il commit cntQuery = UnitOfWorkScope.currentDbContext.Database.SqlQuery <int>("select count(*) from RigheCarrelli where carrello_id = {0}", parametri); int numRigheB = cntQuery.First <int>(); Assert.IsTrue(numRigheB == numRigheA); gestoreCarrello.salvare(); // Eseguo una query sql con un altra connessione, per vedere che la riga sia stata eliminata cntQuery = UnitOfWorkScope.currentDbContext.Database.SqlQuery <int>("select count(*) from RigheCarrelli where carrello_id = {0}", parametri); int numRigheC = cntQuery.First <int>(); Assert.IsTrue(numRigheC == numRigheOrig); } }
private void listRighe_MouseMove(object sender, MouseEventArgs e) { // Questo evento viene generato di continuo. A me interessa soltanto quando è stato fissato il punto inizio drag if (dragStartPoint.X == 0 && dragStartPoint.Y == 0) { return; } // Get the current mouse position Point mousePos = e.GetPosition(null); Vector diff = dragStartPoint - mousePos; bool annulla = false; if (e.LeftButton == MouseButtonState.Pressed) { if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) { // Ricavo la fotografia selezionata ListBox listBoxSorgente = sender as ListBox; RigaCarrello sel = (RigaCarrello)listBoxSorgente.SelectedItem; if (sel != null) { if (sender == this.listRigheStampate) { this.listRigheMasterizzate.AllowDrop = true; this.listRigheStampate.AllowDrop = false; } else if (sender == this.listRigheMasterizzate) { this.listRigheMasterizzate.AllowDrop = false; this.listRigheStampate.AllowDrop = true; } // Initialize the drag & drop operation // Creo una chiave con un valore che mi in DataObject dragData = new DataObject("pollo", sel); DragDrop.DoDragDrop(listBoxSorgente, dragData, DragDropEffects.Move); } else { annulla = true; } } } else { annulla = true; } if (annulla) { annullaDragRighe(); } }
/// <summary> /// Sto per aggiungere una riga nuova al carrello. /// Controllo che non esista già una riga con lo stesso "prodotto" e con la stessa "foto" /// La stessa foto stampata, però può esistere in due formati diversi (prodotti) /// </summary> public static bool isStessaFotoInCarrello(Carrello carrello, RigaCarrello rigaNew) { bool esisteGia = carrello.righeCarrello .Any(r => r.prodotto.Equals(rigaNew.prodotto) && r.fotografia.Equals(rigaNew.fotografia)); return(esisteGia); }
/** * Calcolo il valore della riga * Il valore dello sconto, non è unitario ma è totale */ public static decimal calcValoreRiga(RigaCarrello riga) { decimal _localSconto = riga.sconto != null ? (decimal)riga.sconto : 0; decimal valore = (riga.quantita * riga.prezzoLordoUnitario) - _localSconto; return(valore); }
public void CarrelloTest3EliminaRiga() { #if COSI_FUNZIONA_BENE Guid guid = new Guid("625a1ed1-6e22-4de7-b8c9-2420de1bcb5e"); using (LumenEntities ctx = new LumenEntities()) { Carrello carrello = ctx.Carrelli .Include("righeCarrello") .Single(r => r.id == guid); int conta = 0; RigaCarrello rigaDacanc = null; foreach (var riga in carrello.righeCarrello) { if (++conta == 2) { rigaDacanc = riga; } } if (rigaDacanc != null) { carrello.righeCarrello.Remove(rigaDacanc); ctx.RigheCarrelli.Remove(rigaDacanc); } ctx.SaveChanges(); } #endif // Cerco un carrello che non sia venduto, con almeno due righe Guid guid = Guid.Empty; using (LumenEntities ctx = new LumenEntities()) { Carrello carrello = ctx.Carrelli .Include("righeCarrello") .Where(c => c.venduto == false && c.righeCarrello.Count > 1) .FirstOrDefault(); if (carrello != null) { guid = carrello.id; } } if (guid != Guid.Empty) { // Carrello carrelloTest = recuperaCarrelloPerTest(); using (GestoreCarrello ges = new GestoreCarrello()) { ges.caricaCarrello(guid); RigaCarrello rigaDacanc = ges.carrello.righeCarrello.AsEnumerable().ElementAt(1); ges.removeRiga(rigaDacanc); ges.salvare(); } } }
internal void rimpiazzaFotoInRiga(Guid idFotoDaRefresh) { RigaCarrello riga = carrello.righeCarrello.Where(r => r.fotografia.id == idFotoDaRefresh).SingleOrDefault(); if (riga != null) { ((IObjectContextAdapter)mioDbContext).ObjectContext.Refresh(RefreshMode.StoreWins, riga.fotografia); } isCarrelloModificato = true; }
public void aggiungiRiga(RigaCarrello riga) { if (riga.fotografia == null) { throw new ArgumentNullException("nella RigaCarrello è obbligatoria la Fotografia"); } if (riga.fotografo == null) { throw new ArgumentNullException("nella RigaCarrello è obbligatorio il Fotografo"); } if (riga.prodotto == null) { throw new ArgumentNullException("nella RigaCarrello è obbligatorio il Prodotto"); } if (!isStessaFotoInCarrello(_carrello, riga)) { // Rileggo le associazioni in questo modo gli oggetti vengono riattaccati al context corrente. riga.fotografia = mioDbContext.Fotografie.Single(r => r.id == riga.fotografia.id); riga.fotografo = mioDbContext.Fotografi.Single(f => f.id == riga.fotografo.id); riga.prodotto = mioDbContext.Prodotti.Single(c => c.id == riga.prodotto.id); // Non so perché ma per gestire le associazioni identificanti, (e quindi il cascade dal master al child) occorre sfruttare un attributo con l'ID del padre. // In pratica la FK del figlio deve essere parte della PK (del figlio) quindi una chiave composta (che brutto). // Se non ci credi leggi qui: // http://jamesheppinstall.wordpress.com/2013/06/08/managing-parent-and-child-collection-relationships-in-entity-framework-what-is-an-identifying-relationship-anyway/ riga.carrello = this.carrello; // riga.carrello_id = this.carrello.id; // Però se faccio cosi, non mi funziona piu l'aggiunta di righe ad un carrello esistente !! carrello.righeCarrello.Add(riga); isCarrelloModificato = true; } else { throw new LumenException("La fotografia è già stata caricata nel carrello\r\nModificare la quantità\r\nRiga non aggiunta"); } }
public void crudCarrello2() { using (LumenEntities context = new LumenEntities()) { Random r = new Random(); Carrello cc = new Carrello(); cc.id = Guid.NewGuid(); cc.giornata = DateTime.Today; cc.tempo = DateTime.Today; cc.intestazione = "Test-" + r.Next(1000, 9999); cc.note = "Prova"; cc.prezzoDischetto = r.Next(1, 90); cc.totaleAPagare = r.Next(10, 80); cc.totMasterizzate = (short)r.Next(1, 100); RigaCarrello rc = new RigaCarrello(); rc.id = Guid.NewGuid(); rc.carrello = cc; rc.descrizione = "Test-" + r.Next(1000, 9999); rc.discriminator = "S"; rc.prodotto = context.FormatiCarta.First(); rc.fotografo = context.Fotografi.First(); rc.prezzoLordoUnitario = r.Next(1, 12); rc.quantita = (short)r.Next(1, 12); rc.prezzoNettoTotale = rc.prezzoLordoUnitario * rc.quantita; rc.totFogliStampati = 1; rc.sconto = (decimal)r.Next(1, 12); rc.fotografia = context.Fotografie.First(); rc.bordiBianchi = true; cc.righeCarrello.Add(rc); context.Carrelli.Add(cc); int test = context.SaveChanges(); Assert.IsTrue(test > 0); } }
private void listRighe_Drop(object sender, DragEventArgs e) { if (sender == this.listRigheStampate) { // Ho fatto il drop sulle righe stampate } else if (sender == this.listRigheMasterizzate) { // ho fatto il drop sulle righe masterizzate } var miaVar = e.Data.GetData("pollo"); RigaCarrello rigaDroppata = (RigaCarrello)miaVar; if (carrelloViewModel.SpostaFotoRigaDxSxCommand.CanExecute(rigaDroppata)) { carrelloViewModel.SpostaFotoRigaDxSxCommand.Execute(rigaDroppata); } listRigheMasterizzate.AllowDrop = false; listRigheMasterizzate.AllowDrop = false; }
public void simulaUiStaccando() { Carrello c3; using (LumenEntities dbContext = new LumenEntities()) { c3 = new Carrello { id = Guid.NewGuid(), giornata = DateTime.Today, tempo = DateTime.Now, totaleAPagare = 123m }; c3.righeCarrello = new EntityCollection <RigaCarrello>(); } // ---------- FormatoCarta formato; using (LumenEntities dbContext = new LumenEntities()) { formato = dbContext.FormatiCarta.FirstOrDefault(); } Fotografia fotografia; using (LumenEntities dbContext = new LumenEntities()) { fotografia = dbContext.Fotografie.FirstOrDefault(); } Fotografo fotografo; using (LumenEntities dbContext = new LumenEntities()) { fotografo = dbContext.Fotografi.FirstOrDefault(); } // ---------- using (LumenEntities dbContext = new LumenEntities()) { RigaCarrello r1 = new RigaCarrello(); r1.discriminator = RigaCarrello.TIPORIGA_STAMPA; r1.id = Guid.NewGuid(); r1.prezzoLordoUnitario = new Decimal(5); r1.quantita = 3; r1.prezzoNettoTotale = Decimal.Multiply(r1.prezzoLordoUnitario, r1.quantita); r1.descrizione = "RicaFotoStampata1"; r1.totFogliStampati = 11; r1.prodotto = formato; r1.fotografo = fotografo; r1.fotografia = fotografia; c3.righeCarrello.Add(r1); } // ---------- using (LumenEntities dbContext = new LumenEntities()) { RigaCarrello r1 = c3.righeCarrello.ElementAt(0); // Riattacco le associazioni altrimeti si spacca (sembra) dbContext.FormatiCarta.Attach(r1.prodotto as FormatoCarta); dbContext.Fotografi.Attach(r1.fotografo); dbContext.Fotografie.Attach(r1.fotografia); dbContext.Carrelli.Add(c3); try { dbContext.SaveChanges(); } catch (DbEntityValidationException ee) { string msg = ErroriUtil.estraiMessage(ee); Console.WriteLine(msg); throw; } } }
public void crudCarrello() { Guid guid = Guid.NewGuid(); using (LumenEntities context = new LumenEntities()) { // Prima scrivo un carrello da solo. Carrello c = new Carrello(); c.id = guid; c.giornata = DateTime.Today; c.intestazione = "test1"; c.prezzoDischetto = 123; c.tempo = DateTime.Now; c.venduto = false; c.totaleAPagare = 234; // Creo anche una riga RigaCarrello r = new RigaCarrello(); r.id = Guid.NewGuid(); r.prezzoLordoUnitario = 12.34m; r.quantita = 1; r.descrizione = "test case"; r.prezzoNettoTotale = r.prezzoLordoUnitario; r.fotografia = context.Fotografie.FirstOrDefault(); r.fotografo = r.fotografia.fotografo; r.discriminator = RigaCarrello.TIPORIGA_MASTERIZZATA; c.righeCarrello = new List <RigaCarrello>(); c.righeCarrello.Add(r); // Creo anche un incasso fotografo IncassoFotografo i = new IncassoFotografo(); i.id = Guid.NewGuid(); i.fotografo = r.fotografo; i.incasso = r.prezzoNettoTotale; i.incassoMasterizzate = i.incasso; i.contaMasterizzate = 1; c.incassiFotografi = new List <IncassoFotografo>(); c.incassiFotografi.Add(i); context.Carrelli.Add(c); try { context.SaveChanges(); } catch (DbEntityValidationException qq) { foreach (var item in qq.EntityValidationErrors) { foreach (var item2 in item.ValidationErrors) { String errore = item2.ErrorMessage; Console.WriteLine(errore); } } throw; } catch (Exception) { throw; } } using (LumenEntities context = new LumenEntities()) { Carrello ct = context.Carrelli.Single(c => c.id == guid); Assert.IsTrue(ct.intestazione == "test1"); Assert.IsTrue(ct.righeCarrello.Count == 1); ct.intestazione = "test2"; context.SaveChanges(); } using (LumenEntities context = new LumenEntities()) { Carrello ct = context.Carrelli.Single(c => c.id == guid); Assert.IsTrue(ct.intestazione == "test2"); context.Carrelli.Remove(ct); // Se tutto va bene, mi deve buttare via con il trigger di cascade, anche le RigheCarrello e gli IncassiFotografi context.SaveChanges(); } using (LumenEntities context = new LumenEntities()) { Carrello ct = context.Carrelli.SingleOrDefault(c => c.id == guid); Assert.IsNull(ct); } }
public void carrelloConPezziStaccati() { Carrello c3 = new Carrello { id = Guid.NewGuid(), giornata = DateTime.Today, tempo = DateTime.Now, totaleAPagare = 123m }; c3.righeCarrello = new EntityCollection <RigaCarrello>(); // ---------- FormatoCarta formato; using (LumenEntities dbContext = new LumenEntities()) { formato = dbContext.FormatiCarta.FirstOrDefault(); } Fotografia fotografia; using (LumenEntities dbContext = new LumenEntities()) { fotografia = dbContext.Fotografie.FirstOrDefault(); } Fotografo fotografo; using (LumenEntities dbContext = new LumenEntities()) { fotografo = dbContext.Fotografi.FirstOrDefault(); } // ---------- using (LumenEntities dbContext = new LumenEntities()) { // Creo la riga con gli attributi scalari RigaCarrello r1 = new RigaCarrello { discriminator = RigaCarrello.TIPORIGA_STAMPA, id = Guid.NewGuid(), prezzoLordoUnitario = 5, quantita = 3, prezzoNettoTotale = 15, totFogliStampati = 11, descrizione = "RicaFotoStampata1", }; // Aggiungo le associazioni r1.prodotto = formato; r1.fotografo = fotografo; r1.fotografia = fotografia; // Aggiungo la riga al carrello c3.righeCarrello.Add(r1); } // ---------- using (LumenEntities dbContext = new LumenEntities()) { RigaCarrello r1 = c3.righeCarrello.ElementAt(0); dbContext.Fotografie.Attach(r1.fotografia); dbContext.FormatiCarta.Attach(r1.prodotto as FormatoCarta); dbContext.Fotografi.Attach(r1.fotografo); // The EntityKey property can only be set when the current value of the property is null dbContext.Carrelli.Add(c3); dbContext.SaveChanges(); } }
public void aggiungiUnaRigaAdUnCarrelloEsistente() { int countRigheCarrello = 0; Carrello carrelloCorrente; using (LumenEntities dbContext = new LumenEntities()) { carrelloCorrente = dbContext.Carrelli.Include("righeCarrello").Take(1).Single(); countRigheCarrello = carrelloCorrente.righeCarrello.Count(); } FormatoCarta formato; using (LumenEntities dbContext = new LumenEntities()) { formato = dbContext.FormatiCarta.First(); } Fotografia fotografia; using (LumenEntities dbContext = new LumenEntities()) { fotografia = dbContext.Fotografie.Include("fotografo").Where(f => f.fotografo != null).First(); } Fotografo fotografo; using (LumenEntities dbContext = new LumenEntities()) { fotografo = dbContext.Fotografi.First(); } using (LumenEntities dbContext = new LumenEntities()) { var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; objectContext.AttachTo("Carrelli", carrelloCorrente); ObjectStateEntry s1 = objectContext.ObjectStateManager.GetObjectStateEntry(carrelloCorrente); objectContext.AttachTo("FormatiCarta", formato); ObjectStateEntry s2 = objectContext.ObjectStateManager.GetObjectStateEntry(formato); objectContext.AttachTo("Fotografie", fotografia); ObjectStateEntry s3 = objectContext.ObjectStateManager.GetObjectStateEntry(fotografia); // ======= Occhio qui !!!! poi ti spiego ===== if (fotografo.id.Equals(fotografia.fotografo.id)) { fotografo = fotografia.fotografo; } else { objectContext.AttachTo("Fotografi", fotografo); } ObjectStateEntry s4 = objectContext.ObjectStateManager.GetObjectStateEntry(fotografo); // ======= Occhio qui !!!! poi ti spiego ===== RigaCarrello riga = objectContext.CreateObject <RigaCarrello>(); riga.id = Guid.NewGuid(); riga.discriminator = RigaCarrello.TIPORIGA_STAMPA; riga.prezzoLordoUnitario = new Decimal(5); riga.quantita = 3; riga.prezzoNettoTotale = Decimal.Multiply(riga.prezzoLordoUnitario, riga.quantita); riga.descrizione = "SaveCarrelloLodingTest"; riga.totFogliStampati = 3; riga.prodotto = formato; riga.fotografo = fotografo; riga.fotografia = fotografia; carrelloCorrente.righeCarrello.Add(riga); s1 = objectContext.ObjectStateManager.GetObjectStateEntry(carrelloCorrente); int quanti = dbContext.SaveChanges(); } // Controllo che le righe siano aumentate di uno. using (LumenEntities dbContext = new LumenEntities()) { var testCarrello2 = dbContext.Carrelli.Include("righeCarrello").Where(c => c.id == carrelloCorrente.id).Single(); Assert.IsTrue(countRigheCarrello + 1 == testCarrello2.righeCarrello.Count); } }
public void carrelloTest() { using (LumenEntities dbContext = new LumenEntities()) { Carrello c1 = new Carrello(); c1.id = Guid.NewGuid(); c1.giornata = DateTime.Today; c1.tempo = DateTime.Now; c1.totaleAPagare = 123m; c1.righeCarrello = new EntityCollection <RigaCarrello>(); _carrelloInserito = c1; // --- RigaCarrello r1 = new RigaCarrello(); r1.fotografia = Costanti.findUnaFotografiaRandom(dbContext); r1.fotografo = r1.fotografia.fotografo; r1.discriminator = RigaCarrello.TIPORIGA_MASTERIZZATA; r1.id = Guid.NewGuid(); r1.prezzoLordoUnitario = new Decimal(20); r1.quantita = 2; r1.prezzoNettoTotale = Decimal.Multiply(r1.prezzoLordoUnitario, r1.quantita); r1.descrizione = "Foto masterizzata"; c1.righeCarrello.Add(r1); _contaMasterizzate++; // --- RigaCarrello r2 = new RigaCarrello(); r2.fotografia = Costanti.findUnaFotografiaRandom(dbContext); r2.discriminator = RigaCarrello.TIPORIGA_STAMPA; r2.id = Guid.NewGuid(); r2.prezzoLordoUnitario = new Decimal(5); r2.quantita = 3; r2.prezzoNettoTotale = Decimal.Multiply(r2.prezzoLordoUnitario, r2.quantita); r2.descrizione = "RicaFotoStampata1"; r2.totFogliStampati = 3; r2.prodotto = Utilita.ottieniFormatoCarta(dbContext, "A4"); r2.fotografo = Utilita.ottieniFotografoMario(dbContext); c1.righeCarrello.Add(r2); _contaStampate++; // --- RigaCarrello r3 = new RigaCarrello(); r3.fotografia = Costanti.findUnaFotografiaRandom(dbContext); r3.discriminator = RigaCarrello.TIPORIGA_STAMPA; r3.id = Guid.NewGuid(); r3.prezzoLordoUnitario = new Decimal(5); r3.quantita = 2; r3.prezzoNettoTotale = Decimal.Multiply(r3.prezzoLordoUnitario, r3.quantita); r3.descrizione = "RicaFotoStampata1"; r3.totFogliStampati = 3; r3.prodotto = Utilita.ottieniFormatoCarta(dbContext, "A4"); r3.fotografo = Utilita.ottieniFotografoMario(dbContext); c1.righeCarrello.Add(r3); _contaStampate++; // --- dbContext.Carrelli.Add(c1); try { dbContext.SaveChanges(); } catch (Exception ee) { String msg = ErroriUtil.estraiMessage(ee); Console.WriteLine(msg); throw ee; } } // Verifico che l'inserimento appena effettuato sia andato bene. queryPolimorficaCorrente(); // provo altre tecniche di query, giusto per sport. queryPolimorficaSql(); queryPolimorfica(); }
/** * Se una stampa è andata male, * tramite la riga che è fallita, ricarico il carrello relativo * e lo vado a stornare. */ public void stornoRiga(Guid idRigaCarrello) { if (carrello != null) { throw new InvalidOperationException("Esiste già un carrello caricato"); } using (new UnitOfWorkScope(true)) { LumenEntities dbContext = UnitOfWorkScope.currentDbContext; var query = from c in dbContext.Carrelli.Include("righeCarrello") where c.righeCarrello.Any(q => q.id == idRigaCarrello) select c; carrello = query.SingleOrDefault(); if (carrello == null) { throw new ObjectNotFoundException("La riga carrello con id = " + idRigaCarrello + " non è usabile"); } // Carattere speciale che non c'è sulla tastiera per evitare cancellazioni fraudolente char marca = '\u0251'; RigaCarrello r = carrello.righeCarrello.SingleOrDefault(rr => rr.id == idRigaCarrello); // Devo individuare qual'è la riga da modificare if (r.id != null) { short qtaPrec = r.quantita; decimal totRigaPrec = r.prezzoNettoTotale; // azzero il valore della riga r.quantita = 0; r.prezzoNettoTotale = 0; // decremento il totale a pagare del carrello (evito per qualsiasi ragione di scendere negativo) carrello.totaleAPagare -= totRigaPrec; if (carrello.totaleAPagare < 0) { carrello.totaleAPagare = 0; } if (r.isTipoStampa) { r.descrizione = marca + " Storno " + r.totFogliStampati + " fogli"; r.totFogliStampati = 0; Fotografia f = r.fotografia; f.contaStampata = (short)Math.Max(0, f.contaStampata - qtaPrec); } if (r.isTipoMasterizzata) { r.descrizione = marca + "Storno foto masterizzate"; r.quantita = 0; } completaAttributiMancanti(false); } dbContext.SaveChanges(); } }