Insert data into two tables in database with one to many relationships ASP.NET EF

Viewed 268

I have a Post class

public class Post
    {
        public int Id {get; set;}
        public string Title { get; set; }
        public string Content { get; set; }
        public List<string> GalleryPaths { get; set; }
    }

And an Image class:

    public class Image
    {
        public int Id { get; set; }
        public string Path { get; set; }
        public int PostId { get; set; }
        [ForeignKey("PostId")]
        public virtual Post Post { get; set; }
    }

Now I want to add a Post to my Posts table and a List<Image> to my Images table.

My approach:

public void Create(PostAddRequest model)
        {
            List<string> galleryImageFiles = new List<string>();
            if (model.GalleryImages != null)
            {
                foreach (var item in model.GalleryImages)
                {
                    galleryImageFiles.Add("/Content/img/uploads/" + Guid.NewGuid() + "_" + model.FeaturedImage.FileName);
                    item.SaveAs(System.Web.HttpContext.Current.Server.MapPath(galleryImageFiles.Last()));
                }
            }
            var post = new Post()
            {
                Title = model.Title,
                Content = model.Content,
            };
            model.FeaturedImage.SaveAs(System.Web.HttpContext.Current.Server.MapPath(featuredImageFile));
            _unitOfWork.PostRepository.AddPost(post);
            _unitOfWork.SaveChanges();

            List<Image> gallery = new List<Image>();
            foreach (var item in galleryImageFiles)
            {
                gallery.Add(new Image()
                {
                    Path = item,
                    PostId = post.Id
                });
            }
            _unitOfWork.ImageRepository.AddGallery(gallery);
            _unitOfWork.SaveChanges();
}

I'm kinda new to EF and was wondering if there was a more efficient way of doing this, or even removing the need of a ImageRepository?

1 Answers

I found this answer really helpful

Posting my own solution:

public void Create(PostAddRequest model)
        {
            List<string> galleryImageFiles = new List<string>();
      
            if (model.GalleryImages != null)
            {
                foreach (var item in model.GalleryImages)
                {
                    galleryImageFiles.Add("/Content/img/uploads/" + Guid.NewGuid() + "_" + model.FeaturedImage.FileName);
                    item.SaveAs(System.Web.HttpContext.Current.Server.MapPath(galleryImageFiles.Last()));
                }
            }
            var post = new Post()
            {
                Title = model.Title,
                Content = model.Content,
                GalleryPaths = new List<Image>()
            };

            _unitOfWork.PostRepository.AddPost(post);

            foreach (var item in galleryImageFiles)
            {
                post.GalleryPaths.Add(new Image()
                {
                    Path = item,
                    PostId = post.Id
                });
            }
            _unitOfWork.SaveChanges();
        }
Related