开发者

Duplicate child entries when saving an entity

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();
    }
}


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.


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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜