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 6100511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:23:43+00:00 2026-05-23T13:23:43+00:00

Let’s say I have two EntitySets, Teams and Players. I am adding new teams

  • 0

Let’s say I have two EntitySets, “Teams” and “Players”.

I am adding new teams to the system, for sake of argument, let’s say I’m adding a thousand teams from a file (which contains duplicates).

The system contains 100 teams to start. And my goal is to avoid duplicates without calling SaveChanges() for every team added.

The process is to query that the newteam doesn’t already exist, Then add the new team if not exist.

    var team = (from t in context.Teams
    where t.Name == newTeam.Name
    select t).FirstOrDefault();

if (team==null)
--Add New Team

If I add using context.Teams.AddObject(newTeam); or context.AddObject("Teams",newTeam);

The team.Count() will remain 100 and if you ran the query again, var team would be null.

However, If I instead use player.Teams.Add(newTeam); to add the team, everything works perfectly, except I have to have a player to add the NewTeam to and that player will be a member of all new teams.

After calling player.Teams.Add(newTeam);
The query will return the new team.

 var team = (from t in player.teams
where t.Name = newTeam.Name
select t).FirstOrDefault();

player.Teams.Count() will increase by one.

In the first example context.Teams is an ObjectSet<T>, whereas, in player.Teams.Add Teams is an EntityCollection<T>. I would think these would behave the same, but they do not.

Is there a way to make the ObjectSet<T> behave like the EntityCollection<T>? I want to call SaveChanges once and only once after all teams have been added from the file.

Or is there a better way that I am not seeing?

Thanks!

  • 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-23T13:23:43+00:00Added an answer on May 23, 2026 at 1:23 pm

    No there is no way to make them behave the same. ObjectSet represents database query and once you use it you are always doing query to the database where your new team is not present yet. EntityCollection is local collection of loaded entities and if you use it you are doing query to your application memory.

    Generally using EntityCollection is exactly same as maintaining separate List<Team>:

    List<Team> teams = context.Teams.ToList();
    
    var team = teams.FirstOrDefault(t => t.Name == newTeam.Name);
    if (team == null) 
    {
        context.Teams.AddObject(newTeam);
        teams.Add(newTeam);
    }
    
    context.SaveChanges();
    

    You can also use Dictionary<string, Team> and get probably better performance instead of searching the list for each team.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.