In my MVC application, I have two entities called Ticket
and Attachment
, and when inserting a new ticket record, I also want to insert new records for attachments with the FK of previously inserted Ticket's ID.
I have a look at some samples as this page, but I need to use a loop for the multiple attachments in a single method so that users can attach multiple files when creating a new ticket. Could you give a sample stored procedure or a sample method for Entity Framework to solve this problem?
Here are these two entities:
Ticket:
public class Ticket
{
[Key]
public int ID { get; set; }
public string Comment { get; set; }
//Navigation Property
public virtual ICollection<Attachment> Attachments { get; set; }
}
Attachment:
public class Attachment
{
[Key]
public int ID { get; set; }
//Foreign key for Ticket
public int TicketID { get; set; }
public byte[] FileData { get; set; }
public string FileMimeType { get; set; }
//Navigation Property
public virtual Ticket Ticket { get; set; }
}