Solved

Entity Framework and SaveChanges

Posted on 2010-09-04
1
888 Views
Last Modified: 2012-06-27
Hi,

I am new to the entity framework and have a question, which maybe silly.

public static BlogSeries BLogSeries_Save( BlogSeries source )
        {
            using ( var ctx = new BlogEngineEntities() )
            {
                ctx.BlogSeriesSet.AddObject( source );
                ctx.SaveChanges();
            }
            // What do i do to return the saved source object???
        }

Open in new window


I am trying to create an n-tier application and therefore i am doing some crud operations and what not. I need to return the changed entity after this save operation as this is important as a few things have identity columns and timestamps and what not that are populated by the SQL Server when the item is updated.

My Question therefore is:
After the SaveChanges is run, what do i need to do to return the source object that has now been saved.

0
Comment
Question by:cossy74
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33603202
Hi cossy74;

When you save the changes to the database the local cash of the data gets updated automatically so seeming you have a reference to the record outside the using block all you need to do is return it back. See code snippet.

Fernando
public static BlogSeries BLogSeries_Save( BlogSeries source )
{
    using ( var ctx = new BlogEngineEntities() )
    {
        ctx.BlogSeriesSet.AddObject( source );
        ctx.SaveChanges();
    }
    // What do i do to return the saved source object???
    return source;
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question