I'm using Entity Framework 6, I have Star entity and I'm dealing with following simple code:
foreach(var name in names)
{
Star star = context.Stars
.Where(st => st.Name == name)
.FirstOrDefault();
if (star == null)
{
star = new Star();
star.Name = name;
context.Stars.Add(star);
star = context.Stars
.Where(st => st.Name == name)
.FirstOrDefault();
// Now it's NULL
}
}
I want to add to DB new Star only if it doesn't exists yet. Unfortunatelly, subsequent queries on the same session context without SaveChanges(), cant's find entities added in current session. How can I solve it? (I don't want to save it in each step).