Resolved Adding 2 images to one table, attachment ID only appearing for last function

nearChepstow

Member
Joined
Nov 13, 2020
Messages
6
Programming Experience
1-3
I have 2 images to upload to one table, have got it to the add service and it only adds the one image, so the last attachment insert is overwriting the first one.

Adding 2 images to the table:
var attachment = model.ArticleAttachmentModel.ProcessedFiles.Where(pf => pf.FileByteArray != null).FirstOrDefault();
            var attachmentContentFile = model.ArticleContentFileAttachmentModel.ProcessedFiles.Where(pf => pf.FileByteArray != null).FirstOrDefault();

            if (attachment != null)
            {
                if (attachment.FileByteArray.IsImage())
                {
                    var newAttachment = AttachmentService.NewAttachment(db, attachment.FileByteArray, attachment.Extension, FileContext.ArticleImage, false);
                    newArticleText.Attachment = newAttachment;
                    //newArticleText.Preview_Image_Attachment_ID = newAttachment.ID;
                    newArticleText.Preview_Image_Name = attachment.FileName;
                }
            }

            if (attachmentContentFile != null)
            {
                if (attachmentContentFile.FileByteArray.IsImage())
                {
                    var newContentFileAttachment = AttachmentService.NewAttachment(db, attachmentContentFile.FileByteArray, attachmentContentFile.Extension, FileContext.ArticleContentFile, false);
                    newArticleText.Attachment = model.ConsistsOfSingleFile ? newContentFileAttachment : null;
                    //newArticleText.Main_Content_Attachment_ID = newContentFileAttachment.ID;
                }
            }

            db.SaveChanges();

            return newArticle;
 
the last attachment insert is overwriting the first one.
Of course it is, because that's exactly what you're telling it to do. You do this:
C#:
newArticleText.Attachment = newAttachment;
and then you do this:
C#:
newArticleText.Attachment = model.ConsistsOfSingleFile ? newContentFileAttachment : null;
If you set the same property twice, of course it's only going to have the last value you assign.
 
Back
Top Bottom