.net - How to use Linq to SQL to update certain fields only of entity? -


how use linq sql update fields of entity?

aspx page:

    dim pdf new dashboard.process_pdf()     pdf.id = id     pdf.label = rtblabel.text     pdf.isshared = cbxisshared.checked     pdf.accountid = accountid      if radupload1.uploadedfiles.count > 0  '<-- uploading new file optional in update form required insert (insert works fine)'         dim imagefile uploadedfile = nothing         imagefile = radupload1.uploadedfiles(0)         dim bytes() byte = new byte(imagefile.inputstream.length - 1) {}         imagefile.inputstream.read(bytes, 0, cint(imagefile.inputstream.length))         pdf.filename = imagefile.filename         pdf.filebytes = bytes         pdf.filesize = imagefile.contentlength         pdf.contenttype = imagefile.contenttype         pdf.uploadedby = rusername     end if      pdf.update() 

process_pdf.vb - version1
if have (below) nothing happens. no error. no update.

public sub update()     dc.process_pdfs.attach(me)     dc.submitchanges() end sub 

process_pdf.vb - version2
if have (below) error on attach line:

an entity can attached modified without original state if declares version member or not have update check policy.

public sub update()     dc.process_pdfs.attach(me,true) '<--- errors out here'     dc.submitchanges() end sub 

process_pdf.vb - version3
if have (below) works if update file if don't update file error on submitchanges line (no matter update check settings set to):

cannot insert value null column 'filename', table 'mydbname.dbo.process_pdfs'; column not allow nulls. update fails. statement has been terminated.

public sub update()     dc.process_pdfs.attach(me)     dc.refresh(system.data.linq.refreshmode.keepcurrentvalues, me)     dc.submitchanges() '<--- errors out here if no file update' end sub 

well, cleanest way retrieve entity, update fields need updated, , call submitchanges(). (creating new entity, , copying attributes has disadvantage if new fields added entity need update code, it's not easy maintain)+ anyways linq needs original entity can determine fields updated...


Comments