NHibernate error Reason: Invalid Cast (check your mapping for property type mismatches)

Source: Internet
Author: User
Tags set set

Many people used the nhibernate to operate a one-to-many relationship, using <set> tags, such as:
<set name= "Students",
  <key column= "CId"/
  <one-to-many class= "Nhstusmanagerapp.student"/>
</set>
for use in the corresponding class:
public class classes{
  private , ISet <Student> _students;
  Public virtual  ISet <Student> Students
  {
     get {return _students;}
     set {_students = value;}
 }
}
looks normal, but the runtime will have an error as follows: cannot type ' NHibernate.Collection.Generic.PersistentGenericSet ' 1[ Nhstusmanagerapp.student] "to cast the object to type ' System.Collections.Generic.ISet ' 1[nhstusmanagerapp.student]".

The reason for the error is obviously the type of mistake, that is, the set tag in the configuration file tells NHibernate to use the Set set property in the entity class to load the associated data (e.g. 50 students in a class), and the students attribute in class class is used to store the learner, But you should use this at this time:
Iesi.Collections.Generic ISet, the Iesi.Collections.dll assembly needs to be added.
But if it is not added, it is using the ISetunder System.Collections.Generic .
So there is an error that cannot be converted.

Solution: Use System.Collections.Generic in the class; Comment out, add reference: Using Iesi.Collections.Generic;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.