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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:38:08+00:00 2026-05-15T10:38:08+00:00

I have found many questions here on SO and articles all over the internet

  • 0

I have found many questions here on SO and articles all over the internet but none really tackled my problem.

My model looks like this (I striped all non essential Properties):
alt text

Everyday or so “Play” gets updated (via a XML-file containing the information).

internal Play ParsePlayInfo(XDocument doc)
{
    Play play = (from p in doc.Descendants("Play")
    select new Play
    {
        Theatre = new Theatre()
        {
            //Properties
        },
        //Properties
        LastUpdate = DateTime.Now
    }).SingleOrDefault();

    var actors = (from a in doc.XPathSelectElement(".//Play//Actors").Nodes()
    select new Lecturer()
    {
        //Properties
    });

    var parts = (from p in doc.XPathSelectElement(".//Play//Parts").Nodes()
    select new Part()
    {
        //Properties
    }).ToList();

    foreach (var item in parts)
    {
        play.Parts.Add(item);
    }

    var reviews = (from r in doc.XPathSelectElement(".//Play//Reviews").Nodes()
    select new Review
    {
        //Properties
    }).ToList();

    for (int i = 0; i < reviews.Count(); i++)
    {
        PlayReviews pR = new PlayReviews()
        {
            Review = reviews[i],
            Play = play,
            //Properties
        };
        play.PlayReviews.Add(pR);
    }
    return play;
}

If I add this “play” via Add() every Childobject of Play will be inserted – regardless if some exist already. Since I need to update existing entries I have to do something about that. I guess the problem here is that I have no Id in the XML whatsoever.

As far as I can tell I have the following options:

  1. add/update the child entities in my
    PlayRepositories Add-Method
  2. restructure and rewrite
    ParsePlayInfo() so that get all the
    child entities first, add or update
    them and then create a new Play.
    The only problem I have here is that
    I wanted ParsePlayInfo() to be
    persistence ignorant, I could work
    around this by
  3. creating multiple
    parse methods (eg ParseActors() )
    and assign them to play in my
    controller (I’m using ASP.net MVC)
    after everything was parsed and added

Currently I am implementing option 1 – but it feels wrong.

What I want to do is update entities that are already in the database and insert new ones that aren’t.
Do I have to call SaveChanges() before I Attach/Add Play? There must be a (relatively) easy solution.
I’d appreciate it if someone could guide me in the right direction on this one.

  • 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-15T10:38:08+00:00Added an answer on May 15, 2026 at 10:38 am

    Well, since there is no answer yet I’ll write one myself.

    For those who are wondering, I got it to work – the code looks ugly as hell and I guess performance is even worse. But since there wont be many users and this method will only be called once a day at night anyway I am fine with it for now.

    What did I do?

    Well, I went with option 2 and 3.

    private Play UpdatePlay()
    {
        using (RepositoryContext context = new RepositoryContext())
        {
            HttpRequest http = new HttpRequest();
            PlayRepository rep = new PlayRepository(context);
    
            ActorRepository actRep = new ActorRepository(context);
        ReviewsRepository revRep = new ReviewsRepository(context);
            TheatreRepository insRep = new TheatreRepository(context);
            PartRepository partRep = new PartRepository(context);
    
            Parser p = new Parser();
    
            XDocument doc = http.GetPlayInfo();
    
            Theatre theatre = p.ParseTheatreInfo(doc);
            List<Actor> actors = p.ParseActorInfo(doc);
            List<PlayReviews> playReviews = p.ParseReviewsInfo(doc);
    
            for (int i = 0; i < actors.Count; i++)
            {
                actors[i] = actRep.AddOrUpdate(actors[i]);
            }
            for (int i = 0; i < playReviews.Count; i++)
            {
                playReviews[i].Reviews = revRep.AddOrUpdate(playReviews[i].Reviews);
            }
    
            theatre = insRep.AddOrUpdate(theatre);
    
            Play play = p.ParsePlayInfo(doc);
    
            List<Part> parts = GetParts(play);
    
            for (int i = 0; i < parts.Count; i++)
            {
                List<Actor> lec = (List<Actor>)parts[i].Actors;
                for (int j = 0; j < lec.Count; j++)
                {
                    lec[j] = actRep.AddOrUpdate(lec[j]);
                }
    
            }
    
            play = rep.AddOrUpdate(play);
            context.LoadProperty(play, o => o.Theatre);
            context.LoadProperty(play, o => o.Actors);
            context.LoadProperty(play, o => o.PlayReviewss);
            context.LoadProperty(play, o => o.Parts);
    
            rep.Save();
    
            if (play.Theatre != theatre)
                play.Theatre = theatre;
    
            play = rep.AddParts(parts, play);
    
            play = rep.AddActor(actors, play);
    
            for (int i = 0; i < playReviews.Count; i++)
            {
                playReviews[i].Play = play;
                playReviews[i] = revRep.AddPlayInformation(playReviews[i]);
            }
    
            rep.Save();
            return play;
        }
    }
    

    (And on a side note, I just realized that I previously forgot to post this part of my code…)

    As you can see, Save() gets called twice – and it gets worse when you consider whats going on in AddOrUpdate():

    public Actor AddOrUpdate(Actor entity)
    {
        Actor cur = context.Actors.Where(l => l.Name == entity.Name && l.Last_Name == entity.Last_Name).FirstOrDefault();
        if (cur == null)
        {
            context.Actors.AddObject(entity);
            return entity;
        }
        else
        {
            if (!entity.Mail.IsNullOrEmptyOrWhitespace() && cur.Mail != entity.Mail)
                cur.Mail = entity.Mail;
        //there are more of these...
    
            return cur;
        }
    }
    

    I can’t believe that this is “the right” way to do this.
    It feels and looks just wrong. Maybe EF is to blame too, take

    FirstEntityType first = new FirstEntityType();
    first.Id = 2;
    List<SecondType> list = CreateList(); //Let's say this returns a List with 10 elements
    context.FirstEntityType.AddObject(first);
    
    for (int i = 0; i < list.Count; i++)
    {
        list[i].First = first;
    }
    
    //just for arguments sake a second for
    for (int i = 0; i < list.Count; i++)
    {
        context.SecondType.AddObject(list);
    }
    
    context.SaveChanges();
    

    I haven’t tested this particular piece of code but from what I experienced is that I’ll end up with 10 new entries for SecondType and if I am not wrong 11 for FirstEntityType.

    Why? Why isn’t there a mechanism in EF that says “Hey, wait a minute – those are the same!”

    Am I that wrong thinking that EF should behave like if I was using the db directly? In my example, I added “first” so I could assume that whenever I use “first” it is referenced.
    (I really hope my example works as described – don’t have the time nor the desire to test it)

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

Sidebar

Related Questions

I have found many questions here about storing values in viewstate, but haven't found
i found many related questions here. I have a simple java program . it
I have found many people with simliar issues but no soultions...basically I have two
I have searched for IO issue in forums and found many questions on the
I have seen many somewhat similar questions, but nothing quite what I'm looking for.
I have found many tutorials about using Windows Server 2003 as a development machine,
I have found many similar posts and even tried to find out how to
I am relatively new to assembly language. I have found so many tutorials that
Wondering how to open many new windows with Javascript. I have found plenty of
This is something I've pseudo-solved many times and have never quite found a solution

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.