public async Task <ServiceResult <TextAndImageContent> > CreateContent(string content, ICollection <string> images) { ICollection <BinaryFile> binaryFiles = CheckFiles(images); if (binaryFiles == null) { return(NotFound <TextAndImageContent>(null, "找不到图片")); } else { TextAndImageContent textAndImageContent = new TextAndImageContent { Text = content, }; Context.TextAndImageContents.Add(textAndImageContent); await Context.SaveChangesAsync(); var addtionalImages = binaryFiles.Select( f => new AdditionalImage { ImageFileMD5 = f.MD5, TextAndImageContentId = textAndImageContent.Id, ImageFile = f, TextAndImageContent = textAndImageContent } ).ToList(); Context.AdditionalImages.AddRange(addtionalImages); textAndImageContent.Images = addtionalImages; await Context.SaveChangesAsync(); return(Success(textAndImageContent, "创建成功")); } }
public TextAndImageContent CreateContent(XueLeMeContext context, string text, IEnumerable <string> images) { var content = new TextAndImageContent { Text = text, }; context.TextAndImageContents.Add(content); context.SaveChanges(); foreach (var image in images) { var apply = new AdditionalImage { ImageFileMD5 = image, TextAndImageContentId = content.Id, }; context.AdditionalImages.Add(apply); } context.SaveChanges(); return(content); }