public void InitializeData() { context.Database.Migrate(); var items = readStream(); if (!context.LocalizationRecords.Any()) { localizer.AddNewLocalizationData(items, "New import"); } else { // throws error Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). //localizer.UpdatetLocalizationData(items, "Existing import"); } }
public IActionResult ImportCsvFileForNewData(CsvImportDescription csvImportDescription) { // TODO validate that data is a csv file. var contentTypes = new List <string>(); if (ModelState.IsValid) { foreach (var file in csvImportDescription.File) { if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim('"'); contentTypes.Add(file.ContentType); var inputStream = file.OpenReadStream(); var items = readStream(file.OpenReadStream()); _stringExtendedLocalizerFactory.AddNewLocalizationData(items, csvImportDescription.Information); } } } return(RedirectToAction("Index", "Home")); }
/// <summary> /// Add new Localization Record into Localization DB, with Key, ResourceKey and Culture /// </summary> /// <param name="key">Key of key-value pair in Localization Record</param> /// <param name="text">Value of Key-Value pair in Localization Record</param> /// <param name="resourceKey">The Type of Record, Domain specificity </param> /// <param name="localizationCulture">Translation Language / Current Culture</param> async Task <IJsonResult> ILocalizationRepository.AddLocalizationRecord(string key, string text, string resourceKey, string localizationCulture) { IJsonResult jsonResult = new MyJsonResult() { Message = $"Failed to Add translation for '{key}' to '{text}'", Result = JsonResultFlag.Failed }; LocalizationRecord _resource = await((ILocalizationRepository)this).GetLocalizationRecord(key, resourceKey, localizationCulture); if (_resource == null) { var _sqliteOptions = new DbContextOptionsBuilder <LocalizationModelContext>() .UseSqlite(_SqlContextOption.Value.ConLocalization) .Options; using (var localizationContext = new LocalizationModelContext(_sqliteOptions, _SqlContextOption)) { _resource = new LocalizationRecord() { Key = key, Text = text, ResourceKey = resourceKey, LocalizationCulture = localizationCulture }; var data = new List <LocalizationRecord>(); data.Add(_resource); _SqlLocalizerFactory.AddNewLocalizationData(data, DateTimeOffset.UtcNow.ToString()); // localizationContext.Entry(_resource).State = EntityState.Modified; // await localizationContext.SaveChangesAsync(); jsonResult.Result = JsonResultFlag.Succeeded; jsonResult.Message = $"Successfully added translation for '{key}' to '{text}'"; } } else { jsonResult.Result = JsonResultFlag.Existed; jsonResult.Message = $"Existed translation for '{key}' to '{text}'"; } return(jsonResult); }