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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:18:19+00:00 2026-05-28T20:18:19+00:00

Does it make sense to create new object before trying to save object that

  • 0

Does it make sense to create new object before trying to save object that I already have loaded but with more then one Context?

After lots of tries and errors this code seems to work fine. Basically it’s regetting values for Konsultanci and Szkolenie with the current Context based on objects that are read from ObjectListView.

        var listaDoPodmiany = new List<SzkolenieUczestnicy>();
        if (listaUczestnikow.GetItemCount() > 0) {
            foreach (SzkolenieUczestnicy currentUczestnik in listaUczestnikow.Objects) {
                using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) {
                    if (currentUczestnik.SzkolenieUczestnicyID == 0) {
                        var nowy = new SzkolenieUczestnicy {
                            Konsultanci = context.Konsultancis.First(p => p.KonsultantNazwa == currentUczestnik.Konsultanci.KonsultantNazwa),
                            Szkolenie = context.Szkolenies.First(p => p.SzkolenieID == varSzkolenie.SzkolenieID),
                            SzkolenieUzytkownik = currentUczestnik.SzkolenieUzytkownik,
                            SzkolenieUzytkownikData = currentUczestnik.SzkolenieUzytkownikData,
                            UczestnikPotwierdzilUdzial = currentUczestnik.UczestnikPotwierdzilUdzial,
                            UczestnikPrzybyl = currentUczestnik.UczestnikPrzybyl
                        };


                        context.SzkolenieUczestnicies.AddObject(nowy);
                        context.SaveChanges();
                        listaDoPodmiany.Add(nowy);
                    } else {


                        context.SzkolenieUczestnicies.Attach(currentUczestnik);
                        context.ObjectStateManager.ChangeObjectState(currentUczestnik, EntityState.Modified);
                        context.SaveChanges();
                        listaDoPodmiany.Add(currentUczestnik);
                    }
                }
            }
            listaUczestnikow.ClearObjects();
            listaUczestnikow.SetObjects(listaDoPodmiany);
        }

This seems to solve lots of problems I was getting about multiple contexts. But is it good approach? Will there be much impact on speed since I guess Konsultanci and Szkolenie will be read another time from SQL?

This is continuation (or more like conclusion from multiple questions I asked before):

  • The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
  • Can context of Entity Framework be extracted from object later on?
  • Can Entity Framework context be reused all the time in code?
  • 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-28T20:18:20+00:00Added an answer on May 28, 2026 at 8:18 pm

    Does it make sense to create new object before trying to save object
    that I already have loaded but with more then one Context?

    No. But it shows you have a design problem in your application. You shouldn’t have to deal with multiple contexts for the same objects.

    I think you have a problem with your contexts lifetime. This isn’t an easy issue to solve, but here is an article that could help you. See also here.

    My opinion is that contexts should have the shortest lifetime as possible. Long-running contexts are generally a bad practice, as you’ll quickly encounter issues in your application (memory leak, problems when dealing with multi-threading or concurrency…etc.)

    If you have a 3-tiers architecture, then you really should expose stateless services and create a new context per request. If you’re accessing directly the database from your application (2-tiers application), for instance a Winform application that requests the DB directly, shorten the lifetime of your context as much as possible if you can’t do stateless requests.

    Final word: creating new contexts has a very small overhead, and it won’t lead to performance issues 99% of the time.

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

Sidebar

Related Questions

Does it make sense to create a constant for the value of a penny?
Given a table (id, col1, col2), does it make sense to create the following
Does it make sense to use interfaces for your domain object factories by default,
Does it make sense to use .NET DataSets even in applications that use a
Does it make sense that if the Text on a TextBox is databound to
I'm getting an error that does not make sense. Cross-thread operation not valid: Control
I have an AS3 Object: {p1:.., p2:..} I want con create another object that
What I'm trying is this: 1) Create a new manged object 2) Get it's
Does it make sense, having all of the C#-managed-bliss, to go back to Petzold's
Does it make sense to use SQL Server Compact for a desktop application? How

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.