Solved

Entity Framework and SaveChanges

Posted on 2010-09-04
1
884 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now