Solved

Entity Framework and SaveChanges

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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