public AssetModel Add(AssetModel model) { using (var dbContext = new MissionskyOAEntities()) { if (CheckBarCodeExists(dbContext, model, null)) { throw new Exception("编号已存在."); } var entity = model.ToEntity(); dbContext.Assets.Add(entity); dbContext.SaveChanges(); ///添加属性 if (model.AssetInfoes != null && model.AssetInfoes.Count > 0) { foreach (var item in model.AssetInfoes) { dbContext.AssetInfoes.Add(new AssetInfo() { Asset = entity, AssetId = entity.Id, AttributeId = item.AttributeId, AttributeValue = item.AttributeValue }); } dbContext.SaveChanges(); } return(GetAssetById(entity.Id)); } }
public ActionResult Edit(AssetModel model) { Stream req = Request.InputStream; req.Seek(0, SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); var asset = _assetRepository.GetById(model.Id); if (ModelState.IsValid) { //update status history if (model.AssetStatusId != asset.AssetStatusId) { asset.AssetStatusHistories.Add(new AssetStatusHistory { FromStatus = asset.AssetStatus.Name, ToStatus = _valueItemRepository.GetById(model.AssetStatusId).Name, ChangedUserId = this._workContext.CurrentUser.Id, ChangedDateTime = DateTime.UtcNow }); } //update location history if (model.LocationId != asset.LocationId) { asset.AssetLocationHistories.Add(new AssetLocationHistory { FromLocationId = asset.LocationId, ToLocationId = model.LocationId, ChangedUserId = this._workContext.CurrentUser.Id, ChangedDateTime = DateTime.UtcNow }); } asset = model.ToEntity(asset); //always set IsNew to false when saving asset.IsNew = false; //update attributes _entityAttributeService.UpdateEntityAttributes(model.Id, EntityType.Asset, json); _assetRepository.Update(asset); //commit all changes this._dbContext.SaveChanges(); //notification SuccessNotification(_localizationService.GetResource("Record.Saved")); return(new NullJsonResult()); } else { return(Json(new { Errors = ModelState.SerializeErrors() })); } }
public AssetModel CreateAsset(AssetModel model) { try { model.AssetId = Guid.NewGuid(); model.StatusId = 1; _repo.Asset.Create(model.ToEntity()); _repo.Save(); return(model); } catch (Exception e) { Console.WriteLine(e); throw; } }