public void Add(StockItem stockitem, int userId) { //implement duplicate checking maybe var user = _context.Users.Single(u => u.Id == userId); stockitem.Creator = user; user.StockItemsCreated.Add(stockitem); _context.StockItems.Add(stockitem); _context.Users.Update(user); _context.SaveChanges(); _context.Events.Add(new Event { Date = DateTime.Now, EntityId = stockitem.Id, EntityType = "stock", EventType = "creation" }); _context.SaveChanges(); }
public void Add(User user) { var exist = _context.Users.FirstOrDefault(u => u.Email == user.Email); if (exist != null) { throw new InvalidOperationException("User already exists"); } _context.Users.Add(user); _context.SaveChanges(); _context.Events.Add(new Event { Date = DateTime.Now, EntityId = user.Id, EntityType = "user", EventType = "creation" }); _context.SaveChanges(); }