public virtual TDbEntity Update(TDbEntity Entity) { EntityEntry <TDbEntity> DbEntry = _Context.Attach(_DbSet.Find(GetKey(Entity))); if (typeof(IAuditableUpdate).IsAssignableFrom(Entity.GetType())) { (Entity as IAuditableUpdate).UpdatedOn = DateTimeOffset.Now; (Entity as IAuditableUpdate).UpdatedBy = _SessionServices.UserId; } DbEntry.CurrentValues.SetValues(Entity); // await _Context.SaveChangesAsync(); return(Entity); }
public async void CreateNewCropRun() { ChosenIsVacant = false; var settings = SettingsService.Instance; using (var db = new MainDbContext()) { //TODO: BUG here! Does not check if the CropType already exists correctly, tries to create new one regardless var cropType = _cropTypeCache.FirstOrDefault(ct => ct.Key.Equals(UserCropType.ToLower())).Value; if (cropType == null) { cropType = new CropType { Name = UserCropType, Approved = false, CreatedAt = DateTimeOffset.Now, CreatedBy = settings.CredStableSid }; db.Add(cropType); } else { db.Attach(cropType); } var cropCycle = new CropCycle { ID = Guid.NewGuid(), Name = "Unnamed", Yield = 0, CropType = cropType, CropTypeName = cropType.Name, CropVariety = CropVariety, LocationID = _chosenPlace.Location.ID, CreatedAt = DateTimeOffset.Now, UpdatedAt = DateTimeOffset.Now, StartDate = DateTimeOffset.Now, EndDate = null, Deleted = false, Version = new byte[32] }; db.CropCycles.Add(cropCycle); await db.SaveChangesAsync(); } await BroadcasterService.Instance.TablesChanged.Invoke(string.Empty); }
protected async Task <TEntity> DbCreate(TEntity entity) { var dbEntity = _db.Attach(entity); // Force Added state for cases where the primary key is already set dbEntity.State = EntityState.Added; if (entity is IAuditableEntity) { var user = GetAuthenticatedUser(); var date = DateTime.UtcNow; (entity as IAuditableEntity).CreatedBy = user; (entity as IAuditableEntity).CreatedOn = date; (entity as IAuditableEntity).UpdatedBy = user; (entity as IAuditableEntity).UpdatedOn = date; } await _db.SaveChangesAsync(); return(entity); }
public virtual TObject Update(TObject t) { _context.Attach(t); _context.Entry(t).State = EntityState.Modified; return(t); }