Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Not able to figure out why i get the error "Unable to cast object of type Generic List to type 'NHibernate.Collection.IPersistentCollection'" #497

Unanswered
aswinfrancis asked this question in Q&A
Discussion options

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
{
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().Not.Nullable();
Table("SomeUsers");
}
}
}


During creation of the first Schedule which loads 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.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.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 .
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.