My entities:
UserProfile: - Nothing of importance here.
SupportTicket: - UserProfile
SupportTicketMessage: - UserProfile - SupportTicket
My issue is that whenever I try to insert a SupportTicketMessage I get an additional UserProfile inserted into the database (a duplicate) even though I've attached the corresponding SupportTicket.
Here's my code (inside the SupportTicket class, so this means SupportTicket):
public void AddReply(UserProfile user)
{
SupportTicketMessage msg = new SupportTicketMessage(user, this);
using (DBContext db = new DBContext())
{
db.SupportTickets.Attach(msg.Ticket);
db.SupportTicketMessages.Add(msg);
db.SaveChanges();
}
}
Whenever I run this the SupportTicketMessage gets inserted just fine but it inserts a duplicate UserProfile even though there's a matching one already.
What's the problem here?
Btw here's the supportticketmessage constructor:
public SupportTicketMessage(UserProfile author, SupportTicket ticket)
{
Author = author;
Ticket = ticket;
Date = DateTime.Now;
}