保存实体时重复子条目

发布于 2024-11-29 16:30:28 字数 2377 浏览 2 评论 0原文

更新实体时我收到了子项的重复项。

提交代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

口干舌燥 2024-12-06 16:30:28

尝试将 AsSet 添加到映射中

    HasMany<Document>(x => x.Documents)
        .AsSet()
        .Inverse()
        .Cascade.All();

您需要将 IList 更改为 ICollection 并使用 System.Collections.Generic.HashSet 对其进行初始化。

问题的原因是您可能将同一个文档添加到列表中两次,并且由于未保存它,因此它被插入数据库两次。

Try adding AsSet to the mapping

    HasMany<Document>(x => x.Documents)
        .AsSet()
        .Inverse()
        .Cascade.All();

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.

知足的幸福 2024-12-06 16:30:28

实体的映射我将其作为文档映射:
您应该添加

References<User>(x => x.User).Cascade.None();

Cascade.None() 将停止级联任何更改。

您还可以使用智能感知为您提供在编写 .Cascade 时可以使用的选项。

修订版 2
这应该适用于两个映射

参考文献(x => x.User).Cascade.None();参考文献(x =>
x.Report).Cascade.None();

Mapping of an entity i take it as document map:
you should add

References<User>(x => x.User).Cascade.None();

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

References(x => x.User).Cascade.None(); References(x =>
x.Report).Cascade.None();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文