public void IgnoreEntityDeletedAndIgnoreEntityDublicateKeyError() { var id = Guid.NewGuid(); var id2 = Guid.NewGuid(); using (var cx = new OracleDbContext()) { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); var lw2 = new LastWinsEntity(); lw2.Id = id2; lw2.Name = "v1"; lw2.Created = lw2.Updated = DateTime.Now; cx.LastWins.Add(lw2); cx.SaveChanges(); // add lw1 and lw2 Delete(id); // meantime delete lw1 lw.Name = "v2"; // modify lw1 lw.Updated = DateTime.Now; var lw3 = new LastWinsEntity(); // lw3 dublicate of lw2 lw3.Id = id2; lw2.Name = "v1"; lw2.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw3); cx.SaveChanges(SaveChangesMode.All); } }
public void DublicateKeyHandling_AddUpdate() { var id = Guid.NewGuid(); var lwc = CreateOptiContext(); lwc.SelectFunc = (cx) => { return(cx.LastWins.FirstOrDefault(e => e.Id == id)); }; lwc.AddAction = (cx) => { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v2"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); }; lwc.UpdateAction = (cx, lw) => { lw.Name = "v2"; lw.Updated = DateTime.Now; cx.SaveChanges(); }; lwc.Execute(); // add lwc.Execute(); // update }
public void DublicateKeyHandling_AddDeleteAdd() { var id = Guid.NewGuid(); var lwc = CreateOptiContext(); lwc.SelectFunc = (cx) => { var i = cx.LastWins.Find(id); return(i); }; lwc.AddAction = (cx) => { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); }; lwc.UpdateAction = (cx, lw) => { lw.Name = "v2"; lw.Created = lw.Updated = DateTime.Now; cx.SaveChanges(); }; lwc.Execute(); // add Delete(id); // delete in the meantime lwc.Execute(); // add 2 }
public void IgnoreEntityDeleted2() { var id = Guid.NewGuid(); using (var cx = new OracleDbContext()) { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); // save first one Delete(id); cx.LastWins.Remove(lw); // already deleted cx.SaveChanges(SaveChangesMode.IgnoreEntityDeleted); } }
public void IgnoreEntityDublicateKeyError() { var id = Guid.NewGuid(); using (var cx = new OracleDbContext()) { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); // save first one var lw2 = new LastWinsEntity(); lw2.Id = id; lw2.Name = "v1"; lw2.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw2); cx.SaveChanges(SaveChangesMode.IgnoreEntityDublicateKey); } }
public void DublicateKey_Context() { var id = Guid.NewGuid(); var lwc = CreateOptiContext(); lwc.SelectFunc = (cx) => { var i = cx.LastWins.Find(id); return(i); }; lwc.AddAction = (cx) => { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); }; lwc.UpdateAction = (cx, lw) => { lw.Name = "v2"; lw.Created = lw.Updated = DateTime.Now; cx.SaveChanges(); }; // begin select var cx2 = lwc.ContextFactory(); var entity = lwc.SelectFunc(cx2); // add in the meantime lwc.Execute(); // first add if (entity == null) // entity is always null { lwc.TryAdd(cx2); // simulate add with dublicate key } }
public void DublicateKeyHandling_AddSelectDeleteUpdate() { var id = Guid.NewGuid(); var lwc = CreateOptiContext(); lwc.SelectFunc = (cx) => { var i = cx.LastWins.Find(id); return(i); }; lwc.AddAction = (cx) => { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); }; lwc.UpdateAction = (cx, lw) => { lw.Name = "v2"; lw.Created = lw.Updated = DateTime.Now; cx.SaveChanges(); }; lwc.Execute(); // simulate Execute part 1 -> create context and select entity var cx2 = lwc.ContextFactory(); var entity = lwc.SelectFunc(cx2); // delete in the meantime Delete(id); // simulate Execute part 2 -> TryUpdate the existing entity. Its not here, so AddAction is called lwc.TryUpdate(cx2, entity); }
public void DublicateKey() { var id = Guid.NewGuid(); using (var cx = new OracleDbContext()) { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v1"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); } using (var cx = new OracleDbContext()) { try { var lw = new LastWinsEntity(); lw.Id = id; lw.Name = "v2"; lw.Created = lw.Updated = DateTime.Now; cx.LastWins.Add(lw); cx.SaveChanges(); } catch (DbUpdateException dex) { var oex = dex.InnerException?.InnerException as OracleException; if (oex != null) { if (oex.Number == 1) // 1 = dublicate key error number { throw new Exception("dublicate key", dex); } } } } }