private string CreateProperId(string message, int actionType) { string id = ""; AuditTrail auditTrail; do { id = KeyGenerator.GenerateId(message + actionType + "for-audit"); auditTrail = entities.AuditTrails.Where(m => m.AuditTrailId == id).FirstOrDefault(); if (auditTrail == null) { break; } } while (true); return(id); }
public void SaveImage(HttpPostedFileBase image, string accountId) { string defaultPath = "/Content/Images/User Profile Photo/"; AccountAsset oldAsset = ent.AccountAssets.Where(m => m.AccountId == accountId).FirstOrDefault(); if (oldAsset != null) { LocationStorage oldLocationStorage = oldAsset.LocationStorage; string oldImageAddress = oldLocationStorage.Address; File.Delete(HostingEnvironment.MapPath(oldImageAddress)); ent.AccountAssets.Remove(oldAsset); ent.SaveChanges(); ent.LocationStorages.Remove(oldLocationStorage); ent.SaveChanges(); } string fileName, fileExtension = ""; fileName = Path.GetFileNameWithoutExtension(image.FileName); fileExtension = Path.GetExtension(image.FileName); fileName += accountId; if (IsExisting(defaultPath, fileName, fileExtension)) { fileName += new Random().Next(101); } // variable imgPath would be stored, contains the actual location of the image. string imgPath = defaultPath + fileName; //fileName = Path.Combine(Server.MapPath((defaultPath)), fileName); string filePath = HostingEnvironment.MapPath(defaultPath + fileName + fileExtension); image.SaveAs(filePath); // inserts first to the storage to get the fileId LocationStorage storage = new LocationStorage() { LocationStorageId = KeyGenerator.GenerateId(fileName + accountId), Name = fileName, Address = defaultPath + fileName + fileExtension }; ent.LocationStorages.Add(storage); ent.SaveChanges(); AccountAsset asset = new AccountAsset() { AccountAssetId = KeyGenerator.GenerateId(fileName), AccountId = accountId, LocationStorageId = storage.LocationStorageId }; ent.AccountAssets.Add(asset); ent.SaveChanges(); }