© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
Messiah

❔ EFCore finding a single entry

I need to find an entry in the database or create if it doesn't exist. I can't see any mistakes in my code, yet when I process many chess matches, when an user exists in multiple matches, EF Core keeps adding multiple entries of this same user. Can you see any mistakes in this code? 4 hours of debugging this 😦

var dbAcc = null;
var playerName =entity.properties.RootElement.GetPropertyOrNull("playerName");

if (!string.IsNullOrEmpty(playerName.ToString()))
  queryAccs = queryAccs.Where(e => e.PlayerName== playerName.ToString());

dbAcc = context.Accounts.Find(queryAccs.FirstOrDefault());
if (dbAcc == null)
{
  var newAcc = new AccountEntity
    {
    playerName = playerName.ToString() ?? "error",
    };
  newAcc.ChessTeams.Add(trackedChessTeam);
  context.Accounts.Add(newAcc);
}
else{
  if (!dbAcc.ChessTeams.Contains(trackedChessTeam))
    dbAcc.ChessTeams.Add(trackedChessTeam);
}
break;
var dbAcc = null;
var playerName =entity.properties.RootElement.GetPropertyOrNull("playerName");

if (!string.IsNullOrEmpty(playerName.ToString()))
  queryAccs = queryAccs.Where(e => e.PlayerName== playerName.ToString());

dbAcc = context.Accounts.Find(queryAccs.FirstOrDefault());
if (dbAcc == null)
{
  var newAcc = new AccountEntity
    {
    playerName = playerName.ToString() ?? "error",
    };
  newAcc.ChessTeams.Add(trackedChessTeam);
  context.Accounts.Add(newAcc);
}
else{
  if (!dbAcc.ChessTeams.Contains(trackedChessTeam))
    dbAcc.ChessTeams.Add(trackedChessTeam);
}
break;
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Repository Pattern. EFCore. Single Responsibility (how much of it?)
C#CC# / help
3mo ago
✅ EFCore fetching all data in nearly all tables after a single insert
C#CC# / help
2y ago
SQL EFCore
C#CC# / help
2y ago
❔ efcore querying
C#CC# / help
3y ago