Unable to cast object of type WhereSelectListIterator to type 'NHibernate.Collection.IPersistentCollection'.

284 views
Skip to first unread message

Aswin Francis

unread,
Apr 5, 2021, 1:01:12 PM4/5/21
to nhusers
My parent class:
public class SomeData
{
    public virtual Schedule schedule { get; set; }
    public virtual IEnumerable<SomeUser> SomeUsers{get;set}   
}


Child class:
public class SomeUser : Entity
{
    public virtual SomeData SomeData { get; set; }
    public virtual IUser AssignedTo { get; set; }      
}


SomeData has One-To-Many relation with SomeUser. Mapping is as below:

public class SomeDataMapping : NHibernateClassMapping<SomeData>
{
    public SomeDataMapping()
    {
        References(x => x.Schedule).LazyLoad();
        HasMany(x => x.SomeUser ).Inverse().Cascade.All()
            .KeyColumn("Schedule_Id").Table("SomeUsers");
    }

    public class SomeUserMapping : ClassMap<SomeUser>
    {
        protected SomeUsersMapping()
        {
            Id(x => x.Id);
            References(x => x.SomeData).Column("Schedule_Id").Class<SomeData>().Not.Nullable();
            References(x => x.AssignedTo).Column("AssignedTo_Id").Class<User>().Not.Nullable();
            Table("SomeUsers");
        }
    }
}


During creation of first Schedule which laods these data, it gets saved fine but if I try to edit and save. I get the below error:
System.InvalidCastException: Unable to cast object of type
'WhereSelectListIterator`2[System.Int64,Project.Domain.Schedules.SomeUsers]' to type 'NHibernate.Collection.IPersistentCollection'.
   at NHibernate.Event.Default.FlushVisitor.ProcessCollection(Object collection, CollectionType type)
   at NHibernate.Event.Default.AbstractVisitor.ProcessEntityPropertyValues(Object[] values, IType[] types)
   at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
   at Project.ORM.Core.Configurations.CourseFlushEntityEventListener.OnFlushEntity(FlushEntityEvent e) in

   
Below code is where I map view data to domain object

schedule.SomeData.SomeUsers = viewDto.AssignedUserIds.Select(id => new SomeUser
{
    SomeData = schedule.SomeData,
    AssignedTo = userRepo.FindBy(id)
});


The return type here is IEnumerable. If I convert it to List() and try to save, i get the below error
System.InvalidCastException: Unable to cast object of type
'System.Collections.Generic.List`1[Project.Domain.Schedules.SomeUsers]' to type 'NHibernate.Collection.IPersistentCollection'
.

I have tried setting SomeUser as IEnumberable, ICollection, IList, tried updated the mapping using different option but not able to get over this. I recently implement another HasMany relation mapping the exact same way and it works fine .
Reply all
Reply to author
Forward
0 new messages