public void Delete(int Id) { Vehicle v = null; ProjetasContext _contextInMemory = new ProjetasContext(this.GetDBOptions()); using (_contextInMemory) { v = new Vehicle { Id = 2, Brand = "Nissan", Model = "Skyline GTR R34", Color = "Silver", Year = 1999, Description = "Make movies better", ItsNew = false }; _contextInMemory.Vehicle.Attach(v); _contextInMemory.Vehicle.Remove(v); _contextInMemory.Entry(v).State = EntityState.Deleted; _contextInMemory.SaveChanges(); } Assert.IsNotNull(v); }
public void Update(Vehicle v) { ProjetasContext _contextInMemory = new ProjetasContext(this.GetDBOptions()); v = new Vehicle { Id = 1, Brand = "Dodge", Model = "Charger R/T", Color = "Black", Year = 1970, Description = "I can fly!", ItsNew = false }; using (_contextInMemory) { _contextInMemory.Vehicle.Add(v); Vehicle _dbEntity = _contextInMemory.Vehicle.Find(1); _contextInMemory.Entry(_dbEntity).CurrentValues.SetValues(v); _contextInMemory.SaveChanges(); Assert.IsTrue(v.Id != -1); } }
public static int Update(Vehicle _vehicle) { using (var context = new ProjetasContext()) { Vehicle _dbEntity = context.Vehicle.Find(_vehicle.Id); context.Entry(_dbEntity).CurrentValues.SetValues(_vehicle); context.SaveChanges(); return(_vehicle.Id); } }
public static void Delete(IList <Vehicle> _vehicle) { try { using (var context = new ProjetasContext()) { foreach (Vehicle c in _vehicle) { context.Vehicle.Attach(c); context.Vehicle.Remove(c); context.Entry(c).State = EntityState.Deleted; } context.SaveChanges(); } } catch (Exception dbEx) { string message = string.Format("{0}", dbEx.Message); } }