c# - Use Primary key or Guid as name of images -
i'm working on small cms friend written while ago.
there page user can upload image , display in profile. wonder best way save image. in cms it's goes this: first entity being save database , after getting it's primary key, image being upload server image-{primarykey}.jpg
hits database again update same entity full name of image. here part of code:
in controller:
[httppost] [validateantiforgerytoken] public async task<bool> add([bind(include = "idlanguage,title,address,description,idlinkscategory")]tbllinks tbllinks, string coverpath, string coverbase64) { try { tbllinks.iduser = await _permissionservice.getcurrentiduser(); tbllinks.createddatetime = datetime.now; await _linkservice.add(tbllinks); if (!string.isnullorempty(coverpath)) { byte[] filebyte = _utilityservice.getbytefrombase64string(coverbase64); var filetype = path.getextension(coverpath); var filename = string.format("link-{0}{1}", tbllinks.idlink, filetype); var filemappath = filename.getlinkmappath(); system.io.file.writeallbytes(filemappath, filebyte); tbllinks.imagename = filename; await _linkservice.edit(tbllinks); } return true; } catch { return false; } }
and service layer:
public async task<tbllinks> add(tbllinks links) { _etbllinks.add(links); await _uow.savechangesasync(); _cacheupdateservice.links(); return links; } public async task edit(tbllinks links) { var query = await (from k in _etbllinks k.idlink == links.idlink select k).firstordefaultasync(); query.title = links.title; query.address = links.address; query.description = links.description; query.idlinkscategory = links.idlinkscategory; if (!string.isnullorempty(links.imagename)) query.imagename = links.imagename; _uow.markaschanged(query); await _uow.savechangesasync(); _cacheupdateservice.links(); }
what have in mine , did in of work i'm using guid key name of image, , upload server, use guid key rest of entity , save in database @ once.
so way better? mine or his?
the both valid , don't see 1 being better other (apart second write in db), assuming in solution store image guid/filename property of entity , not using entity key well.
Comments
Post a Comment