Fluent nHibernate HasMany returns duplicated

16 views
Skip to first unread message

Ezequiel De Luca

unread,
Sep 29, 2020, 4:58:19 PM9/29/20
to nhusers
Hi,
Sorry, lately I am arround here a lot, I am trying to create a ManyToOne relationship in nhibernate and I am having a hard time trying to do it. I am trying to link a Request with all the status that it could take during it's life. So far I got the following
public class RequestEntity
{
    public virtual Guid strID { get; set; }
    ...
    public virtual IList<StatusChangeEntity> colStatusChange { get; set; }
}
public class StatusChangeEntity
{
    public virtual Guid strCode { get; set; }
    ...
    public virtual RequestEntity objRequest { get; set; }
}

Mapping:

public RequestMap()
{
    Table("tbl_requests");
    Id(x => x.strID).GeneratedBy.Guid();
    ...
    HasMany<StatusChangeEntity>(x => x.colStatusChange)
        .Cascade.All()
        .KeyColumn("strRequest")
        .Fetch.Join();
}
public StatusChangeMap()
{
    Table("tbl_statuschanges");
    Id(x => x.strCode).GeneratedBy.Guid();
    References(x => x.objRequest)
        .Cascade.All()
        .Class<RequestEntity>()
        .Column("strRequest")
        .Unique();
}

It seems to work as I get the requests with it respective list of status changes, but the list of requests contains duplicates. I got all the request, each one of them with their list of statuschages, but each request with more than one statuschange is return multiple times (one time per status change).Ex:

RequestEntity1(ID=1;statuschanges(statuschange1, statuschange2)),
RequestEntity2(ID=1;statuschanges(statuschange1, statuschange2))
As a workarround when I transform the entities to BO I check the list for duplicates before add them to the list, but I think that it should be already cleaned when it comes from the database.
Do you know what I am skipping?
Reply all
Reply to author
Forward
0 new messages