Solved

Entity Framework and SaveChanges

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
C# TextBox 11 30
How to Decrypt in C# a string that is encrypted with Coldfusion Encrypt(string, seed) function 11 73
Runtime Error 2 28
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

23 Experts available now in Live!

Get 1:1 Help Now