Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:41+00:00 2026-05-23T09:30:41+00:00

Let’s assume I have a file with multiple rows each representing a Person ,

  • 0

Let’s assume I have a file with multiple rows each representing a Person, the Person entity has an identity column that is also the primary key. Assuming that a Person can be repeated in the file, if it is, perhaps I want to do a last entry wins scenario. In the example below I use a repository to retrieve a person from the database by the social security number. The issue is that when the same SSN shows up again in the file, the repository still returns a null, even though technically that person with that SSN has already been added to the context (SaveChanges hasn’t been called yet). I realize I can work around this by tracking myself which Person objects have already been added. I am wondering what is the best practice for this scenario.
Thanks.

foreach(row in fileRows)
{
    Person person = personRepository.GetBySSN(row.SSN);

    if(person == null)
    {
        //insert logic
    }
    else
    {
        //update logic
    }
}

personRepository.SaveChanges();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:30:42+00:00Added an answer on May 23, 2026 at 9:30 am

    I haven’t found anything regarding best practices so I’ll post my solution. I know many other posts refer to the fact that the EF context provides mechanisms for looking into it and seeing if a particular entity is in attached state. Being that I work through repositories (my business layer has no direct access to the EF context), my choice is either offloading this sort of logic into the repository, or attempt to solve it in the business layer. My feeling is that this task is really a business concern and not a data access layer concern.

    Dictionary<string, Person> newPeople = ...
    
    foreach(row in fileRows)
    {
        Person person;
    
        //if the person is already tracked by the dictionary, work with it, if not use the repository
        if(!newPeople.TryGetValue(row.SSN, out person))
        {
            person = personRepository.GetBySSN(row.SSN);
        }
    
        if(person == null)
        {
            //insert logic
    
            //keep track that this person is already in line for inserting
            newPeople.Add(row.SSN, person);
        }
        else
        {
            //update logic
        }
    }
    
    personRepository.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have multiple requirements for a password. The first is that the
Let me try to explain what I need. I have a server that is
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a simple Login servlet that checks the passed name and
Let's assume that we are building a high traffic site that will be used
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have table with column 'URL' whrere I store urls like this
Let's say that I have a set of relations that looks like this: relations
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.