I’m new in asp.net mvc.
I’m using Linq to Sql and trying to do everything loosely coupled.
I’ve two tables:
- News
- NewsFiles
What I’m trying to do is save a news and upload its files at the same time.
How can I create a news in conjunction with his files saving it to NewsFiles table?
The Linq to Sql model is Ok, it includes the object NewsFile to News object.
My concrete repository class for News table (noticia in portuguese):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MagixCMS.Models
{
public class NoticiaRepository : INoticiaRepository
{
#region INoticiaRepository Members
magixcmsEntities _entities = new magixcmsEntities();
public noticia CreateNoticia(noticia noticiaToCreate)
{
_entities.AddTonoticiaSet(noticiaToCreate);
_entities.SaveChanges();
return noticiaToCreate;
}
public void DeletaNoticia(noticia noticiaToDelete)
{
var noticiaOriginal = GetNoticia(noticiaToDelete.Id);
_entities.DeleteObject(noticiaOriginal);
_entities.SaveChanges();
}
public noticia EditNoticia(noticia noticiaToEdit)
{
var noticiaOriginal = GetNoticia(noticiaToEdit.Id);
_entities.ApplyPropertyChanges(noticiaToEdit.EntityKey.EntitySetName, noticiaToEdit);
_entities.SaveChanges();
return noticiaToEdit;
}
public noticia GetNoticia(int id)
{
return (from c in _entities.noticiaSet where c.Id == id select c).FirstOrDefault();
}
public IEnumerable<noticia> ListNoticias()
{
return _entities.noticiaSet.ToList();
}
#endregion
}
}
Look that’s not any mention to NewsFile object.
A strong word of advice: upload/attach the file(s) after the ‘attach target’ record has been created.
I have a similar situation where i have (well almost the same actually) ‘Announcement’ object to which i can attach an image or a PDF attachment. My original idea was to allow the posting of the new Announcement (title, category, body, etc) with the file to upload. Now, admittedly i was trying to design for the upload of many files at the same time (and clever responses/validation for failed uploads).. but the point is: this approach was too hard. Do yourself a favour and have the user create the record first, then attach/upload the file(s) after. Having the primary key table record present will also make things easier.
EDIT: More info about files and uploads:
I use a
Fileobject to store files in my db (so yes i will need a binary/image field for thebyte[]). Then i have other objects that represent concrete files, likeImageandPdfDocthat inherit fromFile– these include other properties (like Width and Height for theImagetype). I have a partial view that renders out a reusable upload control to the detail view which when submitted posts to theAttachImage()orAttachPdfDoc()methods for the particularAnnouncement(after it has been created – see above). The Service layer looks after storing the files, etc after validation and links the objects. Then the detail view reloads and the attached files are listed ready fo download by the public. (NB: This is heavily summarised btw – i also only allow the edit controls (upload form) on the detail view for authenticated users)