Click here to Skip to main content
14,088,376 members
Rate this:
 
Please Sign up or sign in to vote.
Hi I have my code I have written below and i'm not sure it is correct.
I'm passing a record key form my SQL database to delete record(s) and update record(s).
Do I need to put 'db.SaveChanges()' in more often?
Any pointers would be greatfully recieved. I'm new to EF.
Many Thanks.

What I have tried:

Dim db As New iconportalEntities

'Get records in the Artwork Batch
Dim LogRecords = db.ArtworkRequestLog.Where(Function(c) c.F1Int = ukey)

For Each ArtworkRequestLog In LogRecords

    Dim DataLineUK As Integer = ArtworkRequestLog.ItemKey

    'For each line in Artwork Batch: Update related Data line
    Dim DataRecord = db.Datas.SingleOrDefault(Function(c) c.UK = DataLineUK)
    DataRecord.TemplateName = Nothing
    DataRecord.ArtworkRef = ""
    DataRecord.Grade = Nothing

    'Remove Line from Artwork Batch
    Dim ALogRecord = db.ArtworkRequestLog.SingleOrDefault(Function(c) c.UKEY = ukey)
    db.ArtworkRequestLog.Remove(ALogRecord)

Next


'Now Delete the Artwork Request Batch

Dim ABatchRecord = db.ArtworkBatchRequestLog.SingleOrDefault(Function(c) c.UKEY = ukey)
db.ArtworkBatchRequestLog.Remove(ABatchRecord)

db.SaveChanges()
db.Dispose()
Posted
Comments
Richard Deeming 23-Feb-18 9:02am
   
Looks like it should work, apart from the fact that you need to test whether SingleOrDefault actually returned anything.

What happens when you run it?
Member 10103170 23-Feb-18 10:04am
   
Thanks Richard, since writing this question, I managed to find a tutorial that explains that only one SaveChanges is required. Runs OK. Any pointers on a good EF site / book?
thanks, Paul
Karthik_Mahalingam 16-Mar-18 3:06am
   
Tip: use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 23 Feb 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100