保存实体时重复子条目
更新实体时我收到了子项的重复项。
提交代码:
Report report = _ReportService.GetReport(id);
report.AddDocument(
new Document
{
Extension = qqfile.Substring(qqfile.Length - 3),
Path = g.ToString(),
Type = TypeHelper.GetDocumentType(report.Status),
User = MemberFactory.MemberInfo
}
);
report.Status = (ReportStatus)((int)report.Status + 1);
_reportRepository.SaveOrUpdate(report);
public class Document : BaseModel
{
public virtual string Path { get; set; }
public virtual string Extension { get; set; }
public virtual DocumentType Type { get; set; }
public virtual User User { get; set; }
public virtual Report Report { get; set; }
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Id(x=> x.Id);
Map(x=> x.Extension);
Map(x => x.Path);
Map(x => x.CreateDate);
Map(x => x.LastModified);
Map(x => x.Type).CustomType<int>();
References<User>(x => x.User);
References<Report>(x => x.Report);
}
}
public class Report : BaseModel
{
public virtual Patient Patient { get; set; }
public virtual ReportStatus Status { get; set; }
public virtual DateTime AppointmentStart { get; set; }
public virtual DateTime AppointmentEnd { get; set; }
public virtual ReportType Type { get; set; }
public virtual IList<Document> Documents { get; set; }
public virtual long Kareo_Id { get; set; }
public Report()
{
this.Status = ReportStatus.New;
this.Documents = new List<Document>();
}
public virtual void AddDocument(Document document)
{
document.Report = this;
this.Documents.Add(document);
}
}
public class ReportMap : ClassMap<Report>
{
public ReportMap()
{
Id(x => x.Id);
Map(x => x.CreateDate);
Map(x => x.LastModified);
Map(x => x.AppointmentStart);
Map(x => x.AppointmentEnd);
Map(x => x.Type).CustomType<int>();
Map(x => x.Status).CustomType<int>();
Map(x => x.Kareo_Id);
References<Patient>(x => x.Patient);
HasMany<Document>(x => x.Documents)
.Inverse()
.Cascade.All();
}
}
I am getting duplicates of child when updating entity.
Submission Code:
Report report = _ReportService.GetReport(id);
report.AddDocument(
new Document
{
Extension = qqfile.Substring(qqfile.Length - 3),
Path = g.ToString(),
Type = TypeHelper.GetDocumentType(report.Status),
User = MemberFactory.MemberInfo
}
);
report.Status = (ReportStatus)((int)report.Status + 1);
_reportRepository.SaveOrUpdate(report);
public class Document : BaseModel
{
public virtual string Path { get; set; }
public virtual string Extension { get; set; }
public virtual DocumentType Type { get; set; }
public virtual User User { get; set; }
public virtual Report Report { get; set; }
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Id(x=> x.Id);
Map(x=> x.Extension);
Map(x => x.Path);
Map(x => x.CreateDate);
Map(x => x.LastModified);
Map(x => x.Type).CustomType<int>();
References<User>(x => x.User);
References<Report>(x => x.Report);
}
}
public class Report : BaseModel
{
public virtual Patient Patient { get; set; }
public virtual ReportStatus Status { get; set; }
public virtual DateTime AppointmentStart { get; set; }
public virtual DateTime AppointmentEnd { get; set; }
public virtual ReportType Type { get; set; }
public virtual IList<Document> Documents { get; set; }
public virtual long Kareo_Id { get; set; }
public Report()
{
this.Status = ReportStatus.New;
this.Documents = new List<Document>();
}
public virtual void AddDocument(Document document)
{
document.Report = this;
this.Documents.Add(document);
}
}
public class ReportMap : ClassMap<Report>
{
public ReportMap()
{
Id(x => x.Id);
Map(x => x.CreateDate);
Map(x => x.LastModified);
Map(x => x.AppointmentStart);
Map(x => x.AppointmentEnd);
Map(x => x.Type).CustomType<int>();
Map(x => x.Status).CustomType<int>();
Map(x => x.Kareo_Id);
References<Patient>(x => x.Patient);
HasMany<Document>(x => x.Documents)
.Inverse()
.Cascade.All();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 AsSet 添加到映射中
您需要将 IList 更改为 ICollection 并使用 System.Collections.Generic.HashSet 对其进行初始化。
问题的原因是您可能将同一个文档添加到列表中两次,并且由于未保存它,因此它被插入数据库两次。
Try adding AsSet to the mapping
You would need to change IList to ICollection and initialize it with System.Collections.Generic.HashSet.
The cause of the problem is that you are probably adding the same document to the list twice and since it is not saved it gets inserted twice to the db.
实体的映射我将其作为文档映射:
您应该添加
Cascade.None() 将停止级联任何更改。
您还可以使用智能感知为您提供在编写 .Cascade 时可以使用的选项。
修订版 2
这应该适用于两个映射
Mapping of an entity i take it as document map:
you should add
Cascade.None() will stop cascading any changes.
Also you can use intellisense to give you options you can use when you write .Cascade.
Rev 2
this should be for both mappings