get the id of a new record in SaveChanges event on the entityframework

I would like to ask form some help, figuring out how to obtain the Id of a new record added on the entityframework

my function currently:

        Try
            Dim myContext As New Entities
            Dim myRecord As New File
            With myRecord
                .FileTypeCode = GetStringTrimLength(FileTypeCode, 32)
                .FtpShipCode = GetStringTrimLength(FtpShipCode, 15)
                .TransferTypeCode = GetStringTrimLength(inTransferTypeCode, 32)
                .FtpFullPath = GetStringTrimLength(inFtpFullPath, 1024)
                .UserId = GetStringTrimLength(System.Web.Security.Membership.GetUser.UserName, 256)
                .DateTime = Now()
            End With
            myContext.File.AddObject(myRecord)
            myContext.SaveChanges()
        Catch ex As Exception
            'Error Saving (log to event log)
            Shared_Custom.TMSEventLog_Write("Error", ex.ToString)
            Shared_Custom.TMSEventLog_Write("Error", "User=" & GetStringTrimLength(System.Web.Security.Membership.GetUser.UserName, 128) &                                                                       "; EventCode=" & GetStringTrimLength(System.Reflection.MethodBase.GetCurrentMethod.Name, 128) & "; ApplicationCode=" & GetStringTrimLength(My.Application.Info.AssemblyName, 128))
        End Try

' Steps
        '1. Get Id of new file   << here is where I need to have access to the Id of the record created above. 
         Some other steps...

Open in new window


Any help is greatly appreciated.

Thank you.
metropiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
The ID should be populated in your object automatically when it is saved. Try moving the declaration of "myRecord" outside of your Try...Catch so that it is still in scope and check the ID.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metropiaAuthor Commented:
Hi carl_tawn, thank yuo for your comment

I cnahged the code:

        Dim myContext As New OLTPEntities
        Dim myRecord As New File

        Try
            With myRecord
                .FileTypeCode = CQC.StringLibrary.GetStringTrimLength(FileTypeCode, 32)
                .FtpShipCode = CQC.StringLibrary.GetStringTrimLength(FtpShipCode, 15)
                .TransferTypeCode = CQC.StringLibrary.GetStringTrimLength(inTransferTypeCode, 32)
                .FtpFullPath = CQC.StringLibrary.GetStringTrimLength(inFtpFullPath, 1024)
                .LocalFullPath = Convert.DBNull
                .Notes = inFileContents 'TODO: Remove later
                .UserId = CQC.StringLibrary.GetStringTrimLength(System.Web.Security.Membership.GetUser.UserName, 256)
                .DateTime = Now()
            End With
            myContext.File.AddObject(myRecord)
            myContext.SaveChanges()
        Catch ex As Exception
            'Error Saving (log to event log)
            Shared_Custom.TMSEventLog_Write("Error", ex.ToString)
            Shared_Custom.TMSEventLog_Write("Error",
                                            "User=" & CQC.StringLibrary.GetStringTrimLength(System.Web.Security.Membership.GetUser.UserName, 128) &
                                            "; EventCode=" & CQC.StringLibrary.GetStringTrimLength(System.Reflection.MethodBase.GetCurrentMethod.Name, 128) &
                                            "; ApplicationCode=" & CQC.StringLibrary.GetStringTrimLength(My.Application.Info.AssemblyName, 128)
                                           )

        End Try
' Steps
        '1. Get Id of new file   << here is where I need to have access to the Id of the record created above. 
         Some other steps...

Open in new window

.


If that is what you asked me to do, and looks correct, would it be possible that you show me how to get the Id of the newly saved object?

Thank you again for your help.
0
metropiaAuthor Commented:
would it be like?:


Dim FileId As Integer = 0


  myContext.File.AddObject(myRecord)
  myContext.SaveChanges()
  FileId = myRecord.FileId
0
Carl TawnSystems and Integration DeveloperCommented:
Yes. If the field in your object that is the primary key, then once you've call SaveChanges() on the context object it should automatically populate the key field in your object.

So, from the look of it in your case it would be:
Dim FileId As Integer = myRecord.FileId

Open in new window

0
metropiaAuthor Commented:
It works! Excellent!

Thank you a bunch.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.