Programing

NHibernate.Collection.Generic.PersistentGenericBag 유형의 개체를 목록으로 캐스팅 할 수 없습니다.

lottogame 2020. 9. 24. 08:09
반응형

NHibernate.Collection.Generic.PersistentGenericBag 유형의 개체를 목록으로 캐스팅 할 수 없습니다.


ReportRequest라는 클래스가 있습니다.

public class ReportRequest
{
    Int32 templateId;
    List<Int32> entityIds;

    public virtual Int32? Id
    {
        get;
        set;
    }

    public virtual Int32 TemplateId
    {
        get { return templateId; }
        set { templateId = value; }
    }

    public virtual List<Int32> EntityIds
    {
        get { return entityIds; }
        set { entityIds = value; }
    }

    public ReportRequest(int templateId, List<Int32> entityIds)
    {
        this.TemplateId = templateId;
        this.EntityIds = entityIds;
    }
}

Fluent Hibernate를 사용하여 다음과 같이 매핑됩니다.

public class ReportRequestMap : ClassMap<ReportRequest>
{
    public ReportRequestMap()
    {
        Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
        Map(x => x.TemplateId).Not.Nullable();            
        HasMany(x => x.EntityIds).Table("ReportEntities").KeyColumn("ReportRequestId").Element("EntityId").AsBag().Cascade.AllDeleteOrphan();
    }
}

이제이 클래스의 객체를 다음과 같이 만듭니다.

ReportRequest objReportRequest = new ReportRequest(2, new List<int>() { 11, 12, 15 });

다음을 사용하여 데이터베이스에 개체를 저장하십시오.

session.Save(objReportRequest);

다음 오류가 발생합니다. " 'NHibernate.Collection.Generic.PersistentGenericBag 1[System.Int32]' to type 'System.Collections.Generic.List1 [System.Int32]' 형식의 개체를 캐스팅 할 수 없습니다 . "

EntityIds 속성을 올바르게 매핑했는지 확실하지 않습니다. 안내해주세요.

감사합니다!


구체적인 컬렉션 대신 컬렉션 인터페이스를 사용하여 NHibernate가 자체 컬렉션 구현을 삽입 할 수 있도록합니다.

이 경우 IList<int>대신 사용하십시오.List<int>


나는 사용 하지 않는 ICollection<T>곳에서 작동 한다는 것을 알았습니다 IList<T>.

I'm no NHibernate wizard, but I did want to throw a bone to someone else who might land on this issue.

참고URL : https://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag

반응형