Linq To Entity SaveChanges for just one object

I have a c# windows application running on .Net Framework 3.5 and am using Linq to entities to connect to a SQL Server 2008 Express db.

Initially I used seperate contexts for the creation and updates of each db object but I was receiving an error "System.InvalidOperationException: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects".  I am using a single ObjectContext for my single-threaded application and I have read that this recommended ussage as the creation of objectcontexts is expensive.  Is this correct?

Now I have run into the issue that as I am using a single context, when I SaveChanges all my DB objects are updated.  Is there a way to submit just the changes for a single object?  Am I going about this the wrong way?

canuckconsultingAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi canuckconsulting;

See this posting for a better understanding of the issue.

Tip 18 – How to decide on a lifetime for your ObjectContext
http://blogs.msdn.com/alexj/archive/2009/05/07/tip-18-how-to-decide-on-a-lifetime-for-your-objectcontext.aspx

To the question, "Now I have run into the issue that as I am using a single context, when I SaveChanges all my DB objects are updated.  Is there a way to submit just the changes for a single object?  Am I going about this the wrong way?", In fact the only objects sent to the database should be those that were added, modified or deleted all other objects that do not fit this are not sent back to the DB on SaveChanges.

Fernando
0
 
canuckconsultingAuthor Commented:
Hello Fernando and thank you for your response.  

After reading the tip I have changed my code back to using contexts as shown in the code snippet.  So when reading data in I create a context, populate the object and then dispose of it.  When it comes time to save it I create a new object, again load the object, update it to the values and save it using the context.  Does this sound right?

The issue I have is I have tried to encapsulate all of this context stuff within the entities.  But in my code I want to do things like session.JobStatus = activeStatus where both session and activeStatus are entities.  Because each of these were created using different contexts I have to go through the process of creating a context, retrieving the objects again, refrencing them and deleting the context.  It seems to be messy and requires a lot of unnecessary db activity.  Is there a better way or am I missing something?
//Save method of session entity        
    	public void Save()
        {		
            //Create a context
            using (var db = new CanDoAndonEntities())
            {
                //Load a fresh copy of the same session object
                Session res = (from s in db.Session
                               where s.ID == ID
                               select s).FirstOrDefault();
                if (res != null)
                {
                    //Update the object with values of this object
                    res.EndDate = EndDate;
                    res.StartDate = StartDate;
                    //Save changes using this context
                    db.SaveChanges();
                }
            }
        }

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi canuckconsulting;

That looks like exactly what you should be doing. Looks good to me.

Fernando
0
 
canuckconsultingAuthor Commented:
I'm struggling with this.  Say I have one of the Session entities as referenced above.  Each of these session object has a reference to a Status object. If i want to retrieve a Session entity and then update it to a new Status object but not save it what do I do?
0
 
canuckconsultingAuthor Commented:
This helped with the orginal question.  I am opening a fresh ticket for the entity bit I am now struggling with as it is a seperate issue.
0
All Courses

From novice to tech pro — start learning today.