当我保存更改时,一切看起来都很好。CaseWorkNote 实体已正确创建并添加到 workNotes 集合(案例实体的属性)。当 CurrentUnitOfWork 调用 DbContext->SaveChanges() 时,我看到我的实体在那里,状态为已添加。
最后什么都没有保存到数据库中。
我在我的代码中错过了什么或者我做错了什么?
下面是我的代码和带有跟踪实体的屏幕截图。
模型:
public class Case : FullAuditedAggregateRoot<Guid>
{
[Required]
public CaseType Type { get; set; }
[Required]
public string Subject { get; set; }
public string Descripion { get; set; }
//Aggregated entity
private HashSet<CaseWorkNote> _workNotes;
public IEnumerable<CaseWorkNote> WorkNotes => _workNotes?.ToList();
//
public CaseWorkNote AddNote(string text)
{
if (_workNotes is null)
{
_workNotes = new HashSet<CaseWorkNote>();
}
CaseWorkNote workNote = CaseWorkNote.Create(this, text);
_workNotes.Add(workNote);
return workNote;
}
}
public class CaseWorkNote : FullAuditedEntity
{
[ForeignKey("CaseId")]
[Required]
public Case Case { get; private set; }
[Required]
public string Text { get; set; }
private CaseWorkNote() : base() { }
public static CaseWorkNote Create(Case kase, string text)
{
return new CaseWorkNote()
{
Case = kase,
Text = text
};
}
}
幕布斯7119047
慕村9548890
相关分类