Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900