I am creating a context in a method inside my entity to check something but I am not tracking anything with it but when I try to save in the calling code context it throws an exception.
This is the calling code in the main context where I want to save:
var espToProcess = db.RootDomainEmailSeriesProgresses;
foreach (var esp in espToProcess)
{
bool carryOn = esp.MoveNext();
db.SaveChanges(); //Exception
if (!carryOn) continue;
//--> rest of my code
}
This is the methods inside the RootDomainEmailSeriesProgress class.
public bool MoveNext()
{
if (this.CompletedTargets == null) this.CompletedTargets = new List<EmailAddress>();
if (this.CurrentTarget != null)
{
this.CompletedTargets.Add(this.CurrentTarget);
this.CurrentTarget = null;
}
this.CurrentProgress = "";
if (this.RootDomain.ContactFilter != RootDomain.ContactFilterType.None)
{
this.Status = EmailSeriesStatus.Aborted;
return false;
}
var allTargets = RootDomainEmailManager.SortDomainsEmailsByDesirability(this.RootDomain.ID);
var toDo = allTargets.Except(this.CompletedTargets);
if (toDo.Count() < 1)
{
this.Status = EmailSeriesStatus.Completed;
return false;
}
List<string> targetEmailList = allTargets.Select(e => e.Email).ToList();
List<EmailFilter> emailFilters = this.GetFilters(allTargets);
if (emailFilters.Any(x => x.Filter == EmailFilterType.Unsubscribe || x.Filter == EmailFilterType.Responded || x.Filter == EmailFilterType.ManualContactOnly))
{
this.Status = EmailSeriesStatus.Aborted;
if (this.RootDomain.ContactFilter == 0) this.RootDomain.ContactFilter = RootDomain.ContactFilterType.HasAssociatedEmailFilter;
return false;
}
this.CurrentTarget = toDo.First();
return true;
}
private List<EmailFilter> GetFilters(List<EmailAddress> allTargets)
{
using (var db = new PlaceDBContext())
{
db.Configuration.AutoDetectChangesEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
var targetEmailList = allTargets.Select(e => e.Email).ToList();
return db.EmailFilters.AsNoTracking().Where(x => targetEmailList.Contains(x.Email)).ToList();
}
}
It throws out this exception:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
I can't see why esp gets attached to the other context. I only need that context briefly, how do I kill it off so it stops causing me issues?
that because there are difference DbContext instances in foreach loop and GetFilters method
You can retry this code
var espToProcess = db.RootDomainEmailSeriesProgresses;
foreach (var esp in espToProcess)
{
bool carryOn = esp.MoveNext(db);
db.SaveChanges(); //Exception
if (!carryOn) continue;
//--> rest of my code
}
public bool MoveNext(DbContext db)
{
if (this.CompletedTargets == null) this.CompletedTargets = new
List<EmailAddress>();
if (this.CurrentTarget != null)
{
this.CompletedTargets.Add(this.CurrentTarget);
this.CurrentTarget = null;
}
this.CurrentProgress = "";
if (this.RootDomain.ContactFilter != RootDomain.ContactFilterType.None)
{
this.Status = EmailSeriesStatus.Aborted;
return false;
}
var allTargets =
RootDomainEmailManager.SortDomainsEmailsByDesirability(this.RootDomain.ID);
var toDo = allTargets.Except(this.CompletedTargets);
if (toDo.Count() < 1)
{
this.Status = EmailSeriesStatus.Completed;
return false;
}
List<string> targetEmailList = allTargets.Select(e => e.Email).ToList();
List<EmailFilter> emailFilters = this.GetFilters(allTargets, db);
if (emailFilters.Any(x => x.Filter == EmailFilterType.Unsubscribe ||
x.Filter == EmailFilterType.Responded || x.Filter ==
EmailFilterType.ManualContactOnly))
{
this.Status = EmailSeriesStatus.Aborted;
if (this.RootDomain.ContactFilter == 0)
this.RootDomain.ContactFilter =
RootDomain.ContactFilterType.HasAssociatedEmailFilter;
return false;
}
this.CurrentTarget = toDo.First();
return true;
}
private List<EmailFilter> GetFilters(List<EmailAddress> allTargets, DbContext db)
{
db.Configuration.AutoDetectChangesEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
var targetEmailList = allTargets.Select(e => e.Email).ToList();
return db.EmailFilters.AsNoTracking().Where(x =>
targetEmailList.Contains(x.Email)).ToList();
}
Related
Context
I am importing IMDB database files into an SQLite database with the help of EntityFrameworkCore. In fact, two files, the titles.basics and the titles.akas (which is linked to basics via its movie ID).
At first, I had a single thread reading lines from basics and loop through akas until it changes of ID. Though, there was an issue there and most of all, it was too slow. So, I decided to create a multithread code that would read both files at the same time and another combining akas with the appropriate movie.
I am currently importing so I still do not know if my issue is fixed (probably it is). Though, it is still too much slow for me.
Issue
The combining part is still very slow, but more importantly, I can see my process is only using around 12% of CPU which corresponds to only 1/8 of total usage and I have 8 physical cores. So, it really seems the process is only using 1 core.
I am not giving any code here, as having a minimal testable code wouldn't mean anything. Though, you can see both versions here:
https://cints.net/public/Imdb-MultiThread.cs.txt
using com.cyberinternauts.all.MediaRecognizer.Database;
using com.cyberinternauts.all.MediaRecognizer.Models.Metas;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace com.cyberinternauts.all.MediaRecognizer.MetaSources
{
class Imdb : MediaSource
{
private const string TITLES_FILE = "title.basics.tsv.gz";
private const string AKAS_FILE = "title.akas.tsv.gz";
private readonly string temporaryFolder = #"c:\temp\";
private readonly string baseUrl = "https://datasets.imdbws.com/";
private readonly WebClient webClient = new();
MediaRecognizerContext db = new();
private IQueryable<MetaMovie> imdbMovies = null;
private async Task<bool> GatherFilesAsync()
{
var totalFilesGathered = 0;
var filesToDownload = new string[] { AKAS_FILE, TITLES_FILE };
foreach(var fileToDownload in filesToDownload)
{
var compressedFile = temporaryFolder + fileToDownload;
if (!File.Exists(compressedFile) || !File.GetLastWriteTime(compressedFile).Date.Equals(DateTime.Today))
{
await GatherFileAsync(fileToDownload);
totalFilesGathered++;
}
}
return totalFilesGathered != 0;
}
private async Task GatherFileAsync(string fileName)
{
var compressedFile = temporaryFolder + fileName;
var uncompressedFile = temporaryFolder + Path.GetFileNameWithoutExtension(compressedFile);
await webClient.DownloadFileTaskAsync(baseUrl + fileName, compressedFile);
using Stream fd = File.Create(uncompressedFile);
using Stream fs = File.OpenRead(compressedFile);
using Stream csStream = new GZipStream(fs, CompressionMode.Decompress);
var buffer = new byte[1024];
int nRead;
while ((nRead = await csStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fd.WriteAsync(buffer, 0, nRead);
}
}
private async Task LoadMetaDataAsync()
{
//return; //TODO: Remove this line
//TODO: Reactivate this line
//if (!await GatherFilesAsync()) return;
var titlesFile = temporaryFolder + Path.GetFileNameWithoutExtension(TITLES_FILE);
var akasFile = temporaryFolder + Path.GetFileNameWithoutExtension(AKAS_FILE);
var dbLock = new SemaphoreSlim(1);
var akasLock = new SemaphoreSlim(1);
var currentTitlesAkasLock = new SemaphoreSlim(1);
var associateLock = new SemaphoreSlim(1);
using (var db = new MediaRecognizerContext())
{
db.ChangeTracker.AutoDetectChangesEnabled = false;
var titles = new ConcurrentDictionary<string, MetaMovie>();
var readTitles = Task.Factory.StartNew(() =>
{
Parallel.ForEach(File.ReadLines(titlesFile), (titleLine, _, readingIndex) =>
{
if (readingIndex == 0) return; // Skipping columns titles line
var movieInfos = titleLine.Split("\t", StringSplitOptions.None);
dbLock.Wait();
MetaMovie metaMovie = db.MetaMovies.Where(m => m.ExternalId == movieInfos[0]).Include(m => m.Titles).FirstOrDefault();
dbLock.Release();
if (metaMovie == null)
{
int totalMinutes = -1;
if (!int.TryParse(movieInfos[7], out totalMinutes))
{
totalMinutes = -1;
}
metaMovie = new MetaMovie
{
ExternalId = movieInfos[0],
MetaSource = nameof(Imdb),
MovieType = movieInfos[1],
Title = movieInfos[3],
TotalMinutes = totalMinutes,
Genres = movieInfos[8]
};
metaMovie.Titles = new List<MetaTitle>();
if (int.TryParse(movieInfos[5], out int startYear))
{
metaMovie.StartYear = new DateTime(startYear, 1, 1);
}
else
{
metaMovie.StartYear = new DateTime(9999, 1, 1);
}
if (int.TryParse(movieInfos[6], out int endYear))
{
metaMovie.EndYear = new DateTime(endYear, 1, 1);
}
else
{
metaMovie.EndYear = metaMovie.StartYear;
}
}
titles.TryAdd(metaMovie.ExternalId, metaMovie);
});
});
var akas = new Dictionary<string, List<MetaTitle>>();
var currentTitlesAkas = new ConcurrentDictionary<string, int>();
var readAkas = Task.Factory.StartNew(() =>
{
Parallel.ForEach(File.ReadLines(akasFile), (akaLine, _, readingIndex) =>
{
if (readingIndex == 0) return; // Skipping columns titles line
currentTitlesAkasLock.Wait();
var titleInfos = akaLine.Split("\t", StringSplitOptions.None);
var externalId = titleInfos[0];
if (!currentTitlesAkas.ContainsKey(externalId))
{
currentTitlesAkas.TryAdd(externalId, 1);
}
else
{
currentTitlesAkas[externalId]++;
}
currentTitlesAkasLock.Release();
var metaTitle = new MetaTitle
{
MetaMovie = null,
Text = titleInfos[2],
Region = titleInfos[3],
Language = titleInfos[4]
};
akasLock.Wait();
List<MetaTitle> titleAkas;
if (!akas.ContainsKey(externalId))
{
titleAkas = new List<MetaTitle>();
akas.Add(externalId, titleAkas);
}
else
{
titleAkas = akas[externalId];
}
titleAkas.Add(metaTitle);
akasLock.Release();
currentTitlesAkasLock.Wait();
currentTitlesAkas[externalId]--;
currentTitlesAkasLock.Release();
});
});
var savingCounter = 0;
var associate = Task.Factory.StartNew(() =>
{
Parallel.For(1, Environment.ProcessorCount * 10, async (_) =>
{
var isAssociating = true;
do
{
var externalId = string.Empty;
var currentTitleAkaRemoved = false;
currentTitlesAkasLock.Wait();
foreach (var curExternalId in currentTitlesAkas.Keys.OrderBy(t => t))
{
if (currentTitlesAkas[curExternalId] == 0)
{
externalId = curExternalId;
break;
}
}
if (externalId != String.Empty)
{
currentTitleAkaRemoved = currentTitlesAkas.TryRemove(externalId, out int useless0); // Removing so other threads won't take it
}
isAssociating = !readAkas.IsCompleted || !readTitles.IsCompleted || !currentTitlesAkas.IsEmpty;
currentTitlesAkasLock.Release();
if (String.IsNullOrEmpty(externalId) || !currentTitleAkaRemoved) continue;
if (titles.TryGetValue(externalId, out MetaMovie metaMovie))
{
akasLock.Wait();
var titleAkas = akas[externalId];
akas.Remove(externalId);
akasLock.Release();
var changedMovie = false;
var movieAkas = metaMovie.Titles.Select(t => t).ToList(); // Clone list
foreach (var metaTitle in titleAkas)
{
var existingTitle = movieAkas.Where(t => t.Text == metaTitle.Text && t.Region == metaTitle.Region && t.Language == metaTitle.Language).FirstOrDefault();
if (existingTitle == null)
{
changedMovie = true;
metaMovie.Titles.Add(metaTitle);
}
else
{
movieAkas.Remove(existingTitle);
}
}
foreach (var movieTitle in movieAkas)
{
changedMovie = true;
metaMovie.Titles.Remove(movieTitle);
}
dbLock.Wait();
if (metaMovie.Id == 0)
{
db.Add(metaMovie);
}
else if (changedMovie)
{
db.Update(metaMovie);
}
dbLock.Release();
currentTitlesAkasLock.Wait();
currentTitlesAkas.TryRemove(externalId, out int uselessOut); // Free memory
isAssociating = !readAkas.IsCompleted || !readTitles.IsCompleted || !currentTitlesAkas.IsEmpty;
currentTitlesAkasLock.Release();
titles.TryRemove(externalId, out MetaMovie uselessOut2); // Free memory
associateLock.Wait();
savingCounter++;
var localSavingCounter = savingCounter;
associateLock.Release();
if (localSavingCounter != 0 && localSavingCounter % 1000 == 0)
{
var ttt = currentTitlesAkas.Where(t => t.Value > 0);
dbLock.Wait();
await db.SaveChangesAsync();
dbLock.Release();
Console.WriteLine("Saved " + localSavingCounter);
}
}
else if (!readTitles.IsCompleted) // If reading titles is not ended, then maybe it was not read yet... otherwise, it doesn't exist
{
currentTitlesAkasLock.Wait();
currentTitlesAkas.TryAdd(externalId, 0); // Readd because still no movie associated
currentTitlesAkasLock.Release();
}
} while (isAssociating);
});
});
Task.WaitAll(readTitles, readAkas, associate);
await db.SaveChangesAsync();
}
}
public async override Task<IEnumerable<MetaMovie>> FindMediasAsync(DirectoryInfo directory)
{
await LoadMetaDataAsync();
var movie = await ExtractInfosAsync(directory);
if (movie == null) return null;
if (imdbMovies == null)
{
imdbMovies = db.MetaMovies.Where(m => m.MetaSource == nameof(Imdb) && m.MovieType == "movie");
}
return FindCorrespondances(imdbMovies, movie);
}
}
}
https://cints.net/public/Imdb-SingleThread.cs.txt
using com.cyberinternauts.all.MediaRecognizer.Database;
using com.cyberinternauts.all.MediaRecognizer.Models.Metas;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace com.cyberinternauts.all.MediaRecognizer.MetaSources
{
class Imdb : MediaSource
{
private const string TITLES_FILE = "title.basics.tsv.gz";
private const string AKAS_FILE = "title.akas.tsv.gz";
private readonly string temporaryFolder = #"c:\temp\";
private readonly string baseUrl = "https://datasets.imdbws.com/";
private readonly WebClient webClient = new();
MediaRecognizerContext db = new();
private IQueryable<MetaMovie> imdbMovies = null;
private async Task<bool> GatherFilesAsync()
{
var totalFilesGathered = 0;
var filesToDownload = new string[] { AKAS_FILE, TITLES_FILE };
foreach(var fileToDownload in filesToDownload)
{
var compressedFile = temporaryFolder + fileToDownload;
if (!File.Exists(compressedFile) || !File.GetLastWriteTime(compressedFile).Date.Equals(DateTime.Today))
{
await GatherFileAsync(fileToDownload);
totalFilesGathered++;
}
}
return totalFilesGathered != 0;
}
private async Task GatherFileAsync(string fileName)
{
var compressedFile = temporaryFolder + fileName;
var uncompressedFile = temporaryFolder + Path.GetFileNameWithoutExtension(compressedFile);
await webClient.DownloadFileTaskAsync(baseUrl + fileName, compressedFile);
using Stream fd = File.Create(uncompressedFile);
using Stream fs = File.OpenRead(compressedFile);
using Stream csStream = new GZipStream(fs, CompressionMode.Decompress);
var buffer = new byte[1024];
int nRead;
while ((nRead = await csStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fd.WriteAsync(buffer, 0, nRead);
}
}
private async Task LoadMetaDataAsync()
{
//return; //TODO: Remove this line
//TODO: Reactivate this line
//if (!await GatherFilesAsync()) return;
var titlesFile = temporaryFolder + Path.GetFileNameWithoutExtension(TITLES_FILE);
var akasFile = temporaryFolder + Path.GetFileNameWithoutExtension(AKAS_FILE);
var titlesLines = File.ReadLines(titlesFile);
var akasLines = File.ReadLines(akasFile);
var titlesIterator = titlesLines.GetEnumerator();
titlesIterator.MoveNext(); // Skip columns headers
var akasIterator = akasLines.GetEnumerator();
akasIterator.MoveNext();
akasIterator.MoveNext(); // Done twice to skip columns headers
var currentAka = akasIterator.Current;
var savingCounter = 0;
using (var db = new MediaRecognizerContext())
{
db.ChangeTracker.AutoDetectChangesEnabled = false;
while (titlesIterator.MoveNext())
{
var titleLine = titlesIterator.Current;
var movieInfos = titleLine.Split("\t", StringSplitOptions.None);
MetaMovie metaMovie = db.MetaMovies.Where(m => m.ExternalId == movieInfos[0]).FirstOrDefault();
var isNewMovie = false;
if (metaMovie == null)
{
int totalMinutes = -1;
if (!int.TryParse(movieInfos[7], out totalMinutes))
{
totalMinutes = -1;
}
isNewMovie = true;
metaMovie = new MetaMovie
{
ExternalId = movieInfos[0],
MetaSource = nameof(Imdb),
MovieType = movieInfos[1],
Title = movieInfos[3],
TotalMinutes = totalMinutes,
Genres = movieInfos[8]
};
metaMovie.Titles = new List<MetaTitle>();
if (int.TryParse(movieInfos[5], out int startYear))
{
metaMovie.StartYear = new DateTime(startYear, 1, 1);
}
else
{
metaMovie.StartYear = new DateTime(9999, 1, 1);
}
if (int.TryParse(movieInfos[6], out int endYear))
{
metaMovie.EndYear = new DateTime(endYear, 1, 1);
}
else
{
metaMovie.EndYear = metaMovie.StartYear;
}
}
var movieAkasIds = metaMovie.Titles.Select(t => t.Id).ToList();
var titleInfos = currentAka?.Split("\t", StringSplitOptions.None);
while (currentAka != null && int.Parse(titleInfos[0][2..]) <= int.Parse(metaMovie.ExternalId[2..]))
{
if (titleInfos[0] == metaMovie.ExternalId)
{
var metaTitle = new MetaTitle
{
MetaMovie = metaMovie,
Text = titleInfos[2],
Region = titleInfos[3],
Language = titleInfos[4]
};
var existingTitle = metaMovie.Titles.Where(t => t.Text == metaTitle.Text && t.Region == metaTitle.Region && t.Language == metaTitle.Language).FirstOrDefault();
if (existingTitle == null)
{
metaMovie.Titles.Add(metaTitle);
}
else
{
movieAkasIds.Remove(existingTitle.Id);
}
}
else
{
var a = 1;
}
akasIterator.MoveNext();
currentAka = akasIterator.Current;
titleInfos = currentAka.Split("\t", StringSplitOptions.None);
}
foreach(var movieTitleId in movieAkasIds)
{
metaMovie.Titles.Remove(metaMovie.Titles.Where(t => t.Id == movieTitleId).FirstOrDefault());
}
if (isNewMovie)
{
db.Add(metaMovie);
}
else
{
db.Update(metaMovie);
}
savingCounter++;
if (savingCounter % 10000 == 0)
{
await db.SaveChangesAsync();
Console.WriteLine("Saved " + savingCounter);
}
}
await db.SaveChangesAsync();
}
}
public async override Task<IEnumerable<MetaMovie>> FindMediasAsync(DirectoryInfo directory)
{
await LoadMetaDataAsync();
var movie = await ExtractInfosAsync(directory);
if (movie == null) return null;
if (imdbMovies == null)
{
imdbMovies = db.MetaMovies.Where(m => m.MetaSource == nameof(Imdb) && m.MovieType == "movie");
}
return FindCorrespondances(imdbMovies, movie);
}
}
}
In the multithread version, the slow part is in the method LoadMetaDataAsync and more precisely in var associate = Task.Factory.StartNew(() => code part.
This is in development and cleaning, splitting will done after I have the appropriate result/speed.
Case closed. I returned to the single thread version and I found my initial issue (my code was supposing the files were in order, which they were partially).
Thank you for all people that participated.
I am using multi-thread method while saving my email data to database with dapper. There was no problem in my local tests, but it gave an error when I published it on the server.
See my method here, the class I used and the error I got.
How can I solve it? (project is ASP.NET Core 3.1 MVC)
private MailBoxResultDto SaveMails(MailBoxResultDto modelResult)
{
var savedMails = new List<IncomingMailDto>();
var processCount = 0;
int threadCount = 0, maxThreadCount = 50;
foreach (var mail in modelResult.MailList)
{
while (threadCount >= maxThreadCount)
{
Thread.Sleep(100);
}
threadCount++;
var thread = new Thread(() =>
{
// Mail daha önce alınmışsa kaydetme, atla
//var isExistMail = _incomingMailRepository.GetAll()
// .Any(a => a.Date == mail.Date && a.Subject == mail.Subject && a.From == JsonConvert.SerializeObject(mail.MailFrom));
var orm = new DapperOrm(new SqlConnection());
var getMail = orm.QuerySingleOrDefault<IncomingMail>(SqlQueryString.IncomingMailIsExistQueryString(mail.Date, mail.Subject, mail.MailFrom.SerializeObject()));
if (getMail == null)
{
// save mail
var willBeInsertMail = mail.SelectEmailToEntity(attch => attch.SelectAttachmentToEntity(), false);
willBeInsertMail.TenantId = AbpSession.TenantId.Value;
willBeInsertMail.UserId = AbpSession.UserId.Value;
long savedMailID = 0;
//try { savedMailID = _incomingMailRepository.InsertAndGetId(willBeInsertMail); }
orm = new DapperOrm(new SqlConnection());
try { savedMailID = orm.InsertReturnId(willBeInsertMail); }
catch (Exception ex) { threadCount--; processCount++; return; }
// save mail attachments
foreach (var attachment in willBeInsertMail.Attachments)
{
// isim, boyut, değiştirme tarihi, contentType
//var isExistMailAttach = _incomingMailAttachmentRepository.GetAll()
// .Any(a => a.Name == attachment.Name && a.Size == attachment.Size && a.LastModifiedTime == attachment.LastModifiedTime && a.ContentType == attachment.ContentType);
orm = new DapperOrm(new SqlConnection());
var getMailAttachment = orm.QuerySingleOrDefault<IncomingMailAttachment>(SqlQueryString.IncomingMailAttachmentIsExistQueryString(attachment.Name, attachment.Size, attachment.LastModifiedTime, attachment.ContentType));
if (getMailAttachment == null)
{
attachment.MailId = savedMailID;
attachment.TenantId = AbpSession.TenantId.Value;
attachment.Id = 0;
//try { _incomingMailAttachmentRepository.Insert(attachment); }
orm = new DapperOrm(new SqlConnection());
try { orm.Insert(attachment); }
catch (Exception ex) { threadCount--; processCount++; return; }
}
}
var incomingMailDto = willBeInsertMail.SelectEmailToDTO(attach => attach.SelectEmailAttachmentToDTO(), false);
savedMails.Add(incomingMailDto);
}
threadCount--;
processCount++;
});
thread.SetApartmentState(ApartmentState.MTA); // <-- at the this point
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
while (processCount < modelResult.MailList.Count)
{
Thread.Sleep(500);
}
if (savedMails.Count > 1)
return MailBoxResult.Success("Kaydedilen Mail Listesi Getirildi", savedMails);
else
return MailBoxResult.Warning($"Mailler Kaydedilemedi{(string.IsNullOrEmpty(modelResult.ErrorMessage) ? "" : $" : {modelResult.ErrorMessage}")}", null);
}
My Dapper Orm Class
public class DapperOrm
{
public SqlConnection SqlConnection { get; }
public string ConnectionString { get; } = "...sqlconnectionString...";
public DapperOrm(SqlConnection sqlConnection)
{
SqlConnection = sqlConnection;
SqlConnection.ConnectionString = ConnectionString;
}
public DapperOrm(SqlConnection sqlConnection, string connectionString)
{
SqlConnection = sqlConnection;
SqlConnection.ConnectionString = connectionString;
}
public IEnumerable<T> GetQuery<T>(string sqlQuery)
{
IEnumerable<T> result = null;
using (SqlConnection)
{
if (SqlConnection.State != System.Data.ConnectionState.Open)
{
SqlConnection.Close();
SqlConnection.Open();
}
result = SqlConnection.Query<T>(sqlQuery);
SqlConnection.Close();
}
return result;
}
public T QuerySingleOrDefault<T>(string sqlQuery)
{
T result;
using (SqlConnection)
{
if (SqlConnection.State != System.Data.ConnectionState.Open)
{
SqlConnection.Close();
SqlConnection.Open();
}
result = SqlConnection.QuerySingleOrDefault<T>(sqlQuery);
SqlConnection.Close();
}
return result;
}
public bool Insert<T>(T model) where T : IMustHaveTenant
{
bool result = false;
using (SqlConnection)
{
if (SqlConnection.State != System.Data.ConnectionState.Open)
{
SqlConnection.Close();
SqlConnection.Open();
}
var fieldModellessAndListLess = model.GetType().GetProperties()
.Where(s => (
s.PropertyType.BaseType.Name == "ValueType" ||
s.PropertyType.BaseType.Name == "Array" ||
s.PropertyType.Name == "String"
) && s.Name != "Id")
.ToList(); // model ve liste olan propertyler hariç
var tableFields = fieldModellessAndListLess.Select(s => s.Name).ToList();
var fieldNames = $"[{tableFields.Aggregate((a, b) => $"{a}], [{b}")}]";
var valueNames = $"#{tableFields.Aggregate((a, b) => $"{a}, #{b}")}";
result = SqlConnection.Execute($"INSERT INTO {SqlQueryString.GetTableName(model)} ({fieldNames}) VALUES({valueNames})", model) > 0;
SqlConnection.Close();
}
return result;
}
public long InsertReturnId<T>(T model) where T : IMustHaveTenant
{
long result = 0;
using (SqlConnection)
{
if (SqlConnection.State != System.Data.ConnectionState.Open)
{
SqlConnection.Close();
SqlConnection.Open();
}
var fieldModellessAndListLess = model.GetType().GetProperties()
.Where(s => (
s.PropertyType.BaseType.Name == "ValueType" ||
s.PropertyType.BaseType.Name == "Array" ||
s.PropertyType.Name == "String"
) && s.Name != "Id")
.ToList(); // model ve liste olan propertyler hariç
var tableFields = fieldModellessAndListLess.Select(s => s.Name).ToList();
var fieldNames = $"[{tableFields.Aggregate((a, b) => $"{a}], [{b}")}]";
var valueNames = $"#{tableFields.Aggregate((a, b) => $"{a}, #{b}")}";
result = SqlConnection.ExecuteScalar<long>($"INSERT INTO {SqlQueryString.GetTableName(model)} ({fieldNames}) OUTPUT Inserted.ID VALUES({valueNames})", model);
SqlConnection.Close();
}
return result;
}
}
public class SqlQueryString
{
public static string GetTableName<T>(T entity)
{
return $"{entity.GetType().Name}s";
}
public static string IncomingMailIsExistQueryString(DateTime mailDate, string subject, string mailFrom)
{
return $"SELECT TOP 1 Id FROM IncomingMails WHERE [Date] = '{mailDate:yyyy-MM-dd HH:mm:ss}' AND [Subject] = '{subject.Replace("'", "''")}' AND [From] = '{mailFrom.Replace("'", "''")}'";
}
public static string IncomingMailAttachmentIsExistQueryString(string name, int size, DateTime modifiedTime, string contentType)
{
return $"SELECT TOP 1 Id FROM IncomingMailAttachments WHERE [Name] = '{name}' AND [Size] = {size} AND [LastModifiedTime] = '{modifiedTime:yyyy-MM-dd HH:mm:ss}' AND [ContentType] = '{contentType.Replace("'", "''")}'";
}
}
Exception
System.PlatformNotSupportedException: COM Interop is not supported on this platform.
at CFCRM.Mails.Mailbox.MailBoxAppService.SaveMails(MailBoxResultDto modelResult) in /opt/crm/src/CFCRM.Application/Mails/Mailbox/MailBoxAppService.cs:line 343
at CFCRM.Mails.Mailbox.MailBoxAppService.SyncInboxMail() in /opt/crm/src/CFCRM.Application/Mails/Mailbox/MailBoxAppService.cs:line 151
at lambda_method(Closure , Object , Object[] )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
[1
Yes, we found the problem; Our project was running on linux server and I didn't know about it. EWS (Exchange Web Service) also gave an error because there is no linux server support.
Thanks for the comments guys.
I try to implement processing frames from webcam to the WPF application using UWP API.
There is article how to work with MediaCapture & MediaFrameReader:
https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/process-media-frames-with-mediaframereader#handle-the-frame-arrived-event
If I set up MemoryPreference to cpu, SoftwareBitmaps are initialized to the null in the event. When I place Auto, I can see IDirect3DSurface objects are in the event, but in conversion to the SoftwareBitmap the exception "Specified cast is not valid." is raised.
How to convert IDirect3DSurface to SoftwareBitmap?
private async void MediaCaptureExample()
{
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
MediaFrameSourceGroup selectedGroup = null;
MediaFrameSourceInfo colorSourceInfo = null;
foreach (var sourceGroup in frameSourceGroups)
{
foreach (var sourceInfo in sourceGroup.SourceInfos)
{
if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord && sourceInfo.SourceKind == MediaFrameSourceKind.Color)
{
colorSourceInfo = sourceInfo;
break;
}
}
if (colorSourceInfo != null)
{
selectedGroup = sourceGroup;
break;
}
}
capture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedGroup,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
MemoryPreference = MediaCaptureMemoryPreference.Auto,
StreamingCaptureMode = StreamingCaptureMode.Video
};
await capture.InitializeAsync(settings);
var colorFrameSource = capture.FrameSources[colorSourceInfo.Id];
var preferredFormat = colorFrameSource.SupportedFormats.Where(format =>
{
return format.VideoFormat.Width >= 1080
&& String.Compare(format.Subtype, MediaEncodingSubtypes.Mjpg, true) == 0;
}).FirstOrDefault();
if (preferredFormat == null)
{
// Our desired format is not supported
return;
}
await colorFrameSource.SetFormatAsync(preferredFormat);
mediaFrameReader = await capture.CreateFrameReaderAsync(colorFrameSource);
mediaFrameReader.FrameArrived += MediaFrameReader_FrameArrived;
var result = await mediaFrameReader.StartAsync();
Console.WriteLine("Result = " + result.ToString());
}
private void MediaFrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
try
{
var mediaFrameReference = sender.TryAcquireLatestFrame();
var videoMediaFrame = mediaFrameReference?.VideoMediaFrame;
var softwareBitmap = videoMediaFrame?.SoftwareBitmap;
var direct3DSurface = videoMediaFrame?.Direct3DSurface;
if (direct3DSurface != null)
{
var softwareBitmapTask = SoftwareBitmap.CreateCopyFromSurfaceAsync(mediaFrameReference.VideoMediaFrame.Direct3DSurface).AsTask();
softwareBitmap = softwareBitmapTask.Result;
}
if (softwareBitmap != null)
{
using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
var encoderTask = BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream).AsTask();
encoderTask.Wait();
var encoder = encoderTask.Result;
encoder.SetSoftwareBitmap(softwareBitmap);
Task t = encoder.FlushAsync().AsTask();
t.Wait();
var image = new System.Windows.Media.Imaging.BitmapImage();
image.BeginInit();
image.StreamSource = stream.AsStream();
image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
image.EndInit();
imageElement.Source = image;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
The issue was in format subtype. I changed format from Mjpg to Nv12, and everything start working properly (even for MediaCaptureMemoryPreference.Auto):
var preferredFormat = colorFrameSource.SupportedFormats.Where(format =>
{
return format.VideoFormat.Width >= 1080 && String.Compare(format.Subtype, MediaEncodingSubtypes.Nv12, true) == 0;
}).FirstOrDefault();
Let me ask the question differently , is there a way to check if BID=0 convert it to null else //do stuff
I'm adding a new feature on an old code . when a validation met the data is sent through an ajax from view to controller . the data sent correctly , the problem is in the controller as i added an if...else.
Supposedly
if(BID == 0)
{
//implement something
}
else
{
//implement another thing
}
the issue is the condition whether it's false or true , the first statement gets executed and i don't know why . any ideas?
here is the ajax
function SubmitTermination() {
chkVisibility()
validate();
if (EmployeeIsValid && bossIdvalid && ResignationDateIdvalid && PhoneIsvalid) {
var EmployeeID = $('#EmpID').val();
var BossID = $('#MangrID').val();
var RDDate = $('#resignationDate').val();
var RDReason = $('#TerminationType').val();
var RDReasonDetail = $('#terminationDescription').val();
var phone = $('#EmployeePhone').val();
var remember = document.getElementById('IsAgent');
var LDDate = $('#LeavingDate').val();
var isAgent = false;
if (remember.checked) {
isAgent = true;
} else {
isAgent = false;
}
var data = {
UserId: EmployeeID,
BID: BossID,
TDate: RDDate,
LDate: LDDate,
TReason: RDReason,
TPhoneNum: phone,
Agent: isAgent,
TReasonDetail: RDReasonDetail
};
$.ajax({
type: 'POST',
url: '/Resignation/SaveTermination',
daatype: 'json',
data: data,
success: function (result) {
document.getElementById('statusResult').textContent = result;
$('#PopUP').modal('show');
setTimeout(location.reload.bind(location), 4000);
}
});
}
else if (EmployeeIsValid && !bossIdvalid && BossEmpty && ResignationDateIdvalid && PhoneIsvalid) {
var EmployeeID = $('#EmpID').val();
var RDDate = $('#resignationDate').val();
var RDReason = $('#TerminationType').val();
var RDReasonDetail = $('#terminationDescription').val();
var phone = $('#EmployeePhone').val();
var remember = document.getElementById('IsAgent');
var LDDate = $('#LeavingDate').val();
var isAgent = false;
if (remember.checked) {
isAgent = true;
} else {
isAgent = false;
}
var data = {
UserId: EmployeeID,
BID: 0,
TDate: RDDate,
LDate: LDDate,
TReason: RDReason,
TPhoneNum: phone,
Agent: isAgent,
TReasonDetail: RDReasonDetail
};
$.ajax({
type: 'POST',
url: '/Resignation/SaveTermination',
daatype: 'json',
data: data,
success: function (result) {
document.getElementById('statusResult').textContent = result;
$('#PopUP').modal('show');
setTimeout(location.reload.bind(location), 4000);
}
});
}
}
here is the controller with the issue
public JsonResult SaveTermination(int UserId, int BID, string TDate, string LDate, int TReason, Int32 TPhoneNum, bool Agent, string TReasonDetail = "No Details")
{
string result = "nothign yet";
//my try inside the original
#region transfer
//the issue is here
if (BID == 0)
{
bool userInDb = userCheck.checkUserInDB(UserId);
bool BossInDb = false;
bool userAddFromOracle = false;
bool resignationDateCheck = false;
bool leavingDateCheck = false;
bool exsistingResignations = false;
bool ResignationsCount = false;
ExitApplication.Models.User user;
using (db = new ExitApplication2015Entities())
{
user = db.Users.Where(u => u.UserID == UserId).SingleOrDefault();
}
#region Check users in DBS
if (!userInDb)
{
userAddFromOracle = userCheck.GetUserDataFromOracle(UserId.ToString());
if (!userAddFromOracle)
{
result = "Could not find Your User in database or in oracle";
userInDb = false;
}
else
{
userInDb = true;
}
}
#endregion
DateTime resignDate = DateTime.ParseExact(TDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
DateTime leavDate = DateTime.ParseExact(LDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
#region Check the Resignation Date
if (resignDate >= DateTime.Now.Date)
{
resignationDateCheck = true;
}
#endregion
#region Check the Leaving Date
if (leavDate >= DateTime.Now.Date)
{
leavingDateCheck = true;
}
#endregion
var resdt = new Resignation();
using (db = new ExitApplication2015Entities())
{
resdt = db.Resignations.Where(r => r.EmployeeID == UserId && r.OpsRetained == false && r.IsCanceled == false && r.IsCompleted == false && r.IsRejected == false && r.HRRetained == false && r.IsTerminated == false).DefaultIfEmpty().SingleOrDefault();
}
#region Previous Resignation check
if (resdt == null)
{
exsistingResignations = false;
}
else
{
exsistingResignations = true;
}
#endregion
var LeavDateCount = 0;
using (db = new ExitApplication2015Entities())
{
LeavDateCount = db.Resignations.Count(r => r.LeavDate == leavDate);
}
#region Leav Date Count Check
if (LeavDateCount < 30)
{
ResignationsCount = true;
}
else
{
ResignationsCount = false;
}
#endregion
if (userInDb && !BossInDb && BID == 0 && !exsistingResignations && ResignationsCount && leavingDateCheck)
{
Resignation ResignApplication = new Resignation
{
EmployeeID = UserId,
ResignStatusID = 3,
ResignDate = resignDate,
LeavDate = leavDate,
IsActive = true,
IsRejected = false,
OpsRetained = false,
HRRetained = false,
IsTerminated = false,
IsCanceled = false,
IsCompleted = false,
insertedBy = "System",
insertedOn = DateTime.Now,
DepartmentID = user.UserDepartmentID,
AccountOrOUID = (user.UserAccountOUID)
};
using (db = new ExitApplication2015Entities())
{
db.Resignations.Add(ResignApplication);
db.Entry<Resignation>(ResignApplication).State = EntityState.Added;
db.SaveChanges();
}
int resignID = ResignApplication.ResignID;
ResignationDetail resignDetails = new ResignationDetail
{
ResignID = resignID,
ResignReason1 = TReason,
ResignReasonDetail1 = TReasonDetail
};
using (db = new ExitApplication2015Entities())
{
db.ResignationDetails.Add(resignDetails);
db.Entry<ResignationDetail>(resignDetails).State = EntityState.Added;
db.SaveChanges();
}
List<ConcernedParty> ConcernedParties;
using (db = new ExitApplication2015Entities())
{
ConcernedParties = db.ConcernedParties.Where(g => g.IsActive == true).ToList();
}
foreach (ConcernedParty ConcernedParty in ConcernedParties)
{
ResignReleaseDetail relDet = new ResignReleaseDetail
{
/*##*/
ResignID = resignID,
ConcernedPartyID = ConcernedParty.ConcernedPartyID,
ReleaseStatusID = 3,
InsertedBy = Users.CurrentUserId.ToString(),
InsertedOn = DateTime.Now,
IsActive = true
};
using (db = new ExitApplication2015Entities())
{
db.ResignReleaseDetails.Add(relDet);
db.Entry<ResignReleaseDetail>(relDet).State = EntityState.Added;
db.SaveChanges();
}
}
try
{
// Mailing mail = new Mailing(ResignApplication);
// mail.sendMail(1, Int32.Parse(BID.ToString()));
result = "Resignation Submitted Sucessfully";
}
catch
{
result = "The Resignation was submitted but the emails were not sent properly,please contact concerned parties on the mail.";
}
}
else
{
if (!userInDb)
{
result = "Sorry Could not Find your User ID in System database or Oracle database";
}
if (!resignationDateCheck)
{
result = "Sorry you Can not submit a resination with a resign date older than today";
}
if (exsistingResignations)
{
result = "Sorry you have a pending Termination please follow up on it";
}
if (!leavingDateCheck)
{
result = "Sorry you Can not submit a resignation with a Leaving date older than resignation date";
}
if (!ResignationsCount)
{
result = "Resignation Capacity is 30 per day.Sorry you have to choose another Monday or Thursday";
}
}
return Json(result);
}
#endregion
else
{
//string result = "nothign yet";
bool userInDb = userCheck.checkUserInDB(UserId);
bool BossInDb = userCheck.checkUserInDB(BID);
bool userAddFromOracle = false;
bool BossAddFromOracle = false;
bool resignationDateCheck = false;
bool leavingDateCheck = false;
bool exsistingResignations = false;
bool ResignationsCount = false;
ExitApplication.Models.User user;
using (db = new ExitApplication2015Entities())
{
user = db.Users.Where(u => u.UserID == UserId).SingleOrDefault();
}
#region Check users in DBS
if (!userInDb)
{
userAddFromOracle = userCheck.GetUserDataFromOracle(UserId.ToString());
if (!userAddFromOracle)
{
result = "Could not find Your User in database or in oracle";
userInDb = false;
}
else
{
userInDb = true;
}
}
if (!BossInDb)
{
BossAddFromOracle = userCheck.GetUserDataFromOracle(BID.ToString());
if (!BossAddFromOracle)
{
result = "Could not find Your Boss User in database or in oracle";
BossInDb = false;
}
else
{
BossInDb = true;
}
}
#endregion
DateTime resignDate = DateTime.ParseExact(TDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
DateTime leavDate = DateTime.ParseExact(LDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
#region Check the Resignation Date
if (resignDate >= DateTime.Now.Date)
{
resignationDateCheck = true;
}
#endregion
#region Check the Leaving Date
if (leavDate >= DateTime.Now.Date)
{
leavingDateCheck = true;
}
#endregion
var resdt = new Resignation();
using (db = new ExitApplication2015Entities())
{
resdt = db.Resignations.Where(r => r.EmployeeID == UserId && r.OpsRetained == false && r.IsCanceled == false && r.IsCompleted == false && r.IsRejected == false && r.HRRetained == false && r.IsTerminated == false).DefaultIfEmpty().SingleOrDefault();
}
#region Previous Resignation check
if (resdt == null)
{
exsistingResignations = false;
}
else
{
exsistingResignations = true;
}
#endregion
var LeavDateCount = 0;
using (db = new ExitApplication2015Entities())
{
LeavDateCount = db.Resignations.Count(r => r.LeavDate == leavDate);
}
#region Leav Date Count Check
if (LeavDateCount < 30)
{
ResignationsCount = true;
}
else
{
ResignationsCount = false;
}
#endregion
if (userInDb && BossInDb && !exsistingResignations && ResignationsCount && leavingDateCheck)
{
Resignation ResignApplication = new Resignation
{
EmployeeID = UserId,
ManagerID = BID,
ResignStatusID = 1,
ResignDate = resignDate,
LeavDate = leavDate,
IsActive = true,
IsRejected = false,
OpsRetained = false,
HRRetained = false,
IsTerminated = false,
IsCanceled = false,
IsCompleted = false,
insertedBy = "System",
insertedOn = DateTime.Now,
DepartmentID = user.UserDepartmentID,
AccountOrOUID = (user.UserAccountOUID)
};
using (db = new ExitApplication2015Entities())
{
db.Resignations.Add(ResignApplication);
db.Entry<Resignation>(ResignApplication).State = EntityState.Added;
db.SaveChanges();
}
int resignID = ResignApplication.ResignID;
ResignationDetail resignDetails = new ResignationDetail
{
ResignID = resignID,
ResignReason1 = TReason,
ResignReasonDetail1 = TReasonDetail
};
using (db = new ExitApplication2015Entities())
{
db.ResignationDetails.Add(resignDetails);
db.Entry<ResignationDetail>(resignDetails).State = EntityState.Added;
db.SaveChanges();
}
List<ConcernedParty> ConcernedParties;
using (db = new ExitApplication2015Entities())
{
ConcernedParties = db.ConcernedParties.Where(g => g.IsActive == true).ToList();
}
foreach (ConcernedParty ConcernedParty in ConcernedParties)
{
ResignReleaseDetail relDet = new ResignReleaseDetail
{
/*##*/
ResignID = resignID,
ConcernedPartyID = ConcernedParty.ConcernedPartyID,
ReleaseStatusID = 3,
InsertedBy = Users.CurrentUserId.ToString(),
InsertedOn = DateTime.Now,
IsActive = true
};
using (db = new ExitApplication2015Entities())
{
db.ResignReleaseDetails.Add(relDet);
db.Entry<ResignReleaseDetail>(relDet).State = EntityState.Added;
db.SaveChanges();
}
}
try
{
// Mailing mail = new Mailing(ResignApplication);
// mail.sendMail(1, Int32.Parse(BID.ToString()));
result = "Resignation Submitted Sucessfully";
}
catch
{
result = "The Resignation was submitted but the emails were not sent properly,please contact concerned parties on the mail.";
}
}
else
{
if (!userInDb)
{
result = "Sorry Could not Find your User ID in System database or Oracle database";
}
if (!BossInDb)
{
result = "Sorry Could not Find your Direct Manager ID in System database or Oracle database";
}
if (!resignationDateCheck)
{
result = "Sorry you Can not submit a resination with a resign date older than today";
}
if (exsistingResignations)
{
result = "Sorry you have a pending Termination please follow up on it";
}
if (!leavingDateCheck)
{
result = "Sorry you Can not submit a resignation with a Leaving date older than resignation date";
}
if (!ResignationsCount)
{
result = "Resignation Capacity is 30 per day.Sorry you have to choose another Monday or Thursday";
}
}
return Json(result);
}
}
Error:
Store update, insert, or delete statement affected an unexpected
number of rows (0). Entities may have been modified or deleted since
entities were loaded.
I am trying to upload a file into my database with few changes in it but each time it keeps giving me this error and won't update the values...
if (!addRecord) {
if (currentShiipingAddress != null) {
var shippingAddress = existingAccount.AccountAddresses.FirstOrDefault(m => m.Name == currentShiipingAddress.Name && m.AddressLine1 == currentShiipingAddress.AddressLine1 && m.AddTypeShipping == true);
if (shippingAddress == null) {
existingAccount.AccountAddresses.Add(currentShiipingAddress);
}
else {
shippingAddress.PostCode = currentShiipingAddress.PostCode;
shippingAddress.AddressLine1 = currentShiipingAddress.AddressLine1;
shippingAddress.AddressLine2 = currentShiipingAddress.AddressLine2;
shippingAddress.AddressLine3 = currentShiipingAddress.AddressLine3;
shippingAddress.DateUpdated = DateTime.UtcNow;
shippingAddress.AddTypeShipping = true;
context.Entry(shippingAddress).State = EntityState.Modified;
}
}
if (currentBillingAddress != null) {
var billingAddress = existingAccount.AccountAddresses.FirstOrDefault(m => m.Name == currentBillingAddress.Name && m.AddressLine1 == currentBillingAddress.AddressLine1 && m.AddTypeBilling == true);
if (billingAddress == null) {
existingAccount.AccountAddresses.Add(currentBillingAddress);
}
else {
billingAddress.PostCode = currentBillingAddress.PostCode;
billingAddress.AddressLine1 = currentBillingAddress.AddressLine1;
billingAddress.AddressLine2 = currentBillingAddress.AddressLine2;
billingAddress.AddressLine3 = currentBillingAddress.AddressLine3;
billingAddress.DateUpdated = DateTime.UtcNow;
billingAddress.AddTypeBilling = true;
context.Entry(billingAddress).State = EntityState.Modified;
}
}
if (invoiceCurrentContact != null) {
var existingContact = existingAccount.AccountContacts.FirstOrDefault(m => m.ContactEmail == invoiceEmail);
if (existingContact == null) {
existingAccount.AccountContacts.Add(invoiceCurrentContact);
}
else {
existingContact.ContactEmail = invoiceCurrentContact.ContactEmail;
existingContact.ContactName = invoiceCurrentContact.ContactName;
existingContact.TenantContactPhone = invoiceCurrentContact.TenantContactPhone;
existingContact.DateUpdated = DateTime.UtcNow;
context.Entry(existingContact).State = EntityState.Modified;
}
}
if (purchaseCurrentContact != null) {
var existingContact = existingAccount.AccountContacts.FirstOrDefault(m => m.ContactEmail == purchaseEmail);
if (existingContact == null) {
existingAccount.AccountContacts.Add(purchaseCurrentContact);
}
else {
existingContact.ContactEmail = purchaseCurrentContact.ContactEmail;
existingContact.ContactName = purchaseCurrentContact.ContactName;
existingContact.TenantContactPhone = purchaseCurrentContact.TenantContactPhone;
existingContact.DateUpdated = DateTime.UtcNow;
context.Entry(existingContact).State = EntityState.Modified;
}
}
}
else {
if (invoiceCurrentContact != null) {
existingAccount.AccountContacts.Add(invoiceCurrentContact);
}
if (purchaseCurrentContact != null) {
existingAccount.AccountContacts.Add(purchaseCurrentContact);
}
if (currentShiipingAddress != null) {
existingAccount.AccountAddresses.Add(currentShiipingAddress);
}
if (currentBillingAddress != null) {
existingAccount.AccountAddresses.Add(currentBillingAddress);
}
}
if (addRecord) {
context.Account.Add(existingAccount);
}
else {
context.Entry(existingAccount).State = EntityState.Modified;
}
This is where I am committing the changes to be saved but it turns out to give me the above-mentioned error
}
context.SaveChanges();
}
}
}
catch (Exception ex)
{
return "Import Failed : " + ex.Message + " " + counter.ToString();
}
return $"Supplier Account details imported successfully. Added { addedSuppliers }, Updated = { updatedSuppliers }";
}