我有以下代码,我想在保存之前添加两个帖子条目:
ticket.Open();
var post = new Post
{
TicketId = dto.TicketId,
PostContent = "Ticket status changed to Open",
IsAutomatic = true
};
ticket.AddPost(post);
post = new Post
{
TicketId = dto.TicketId,
PostContent = HttpUtility.HtmlEncode(dto.Comments)
};
ticket.AddPost(post);
var notification = Notification.TicketReopened(post);
_unitOfWork.Complete();
帖子属于工单。通知是关于帖子的。
但是我遇到以下错误:
Unable to determine the principal end of the 'Notification_Post' relationship. Multiple added entities may have the same primary key.
我唯一可以解决的方法是在添加第一条帖子后调用_unitOfWork.Complete()
,然后在末尾再次调用它。
_unitOfWork.Complete()
只需执行以下操作:
public void Complete()
{
_context.SaveChanges();
}
但是,我需要整体完成整个交易。
我该如何解决?