i am getting exception like "NHibernate:Exception occurred getter of Core.Model.Country.CountryID ({"Object does not match target type"})" in VS 2010 MVC..
Hello,
i am facing problem in MVC3 (Razor) and NHibernate.
i having five Models like
Country
State
District
Tehsil
Student
and mapping file of each..codes of Model and Mapping file is like below..
Country.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Core.Model
{
public class Country
{
[Key]
public virtual Int32 CountryID { get; set; }
[Display(Name = "Country Name")]
public virtual string Countryname { get; set; }
[Display(Name = "State List")]
public virtual ICollection<State> StateList { get; set; }
}
}
Code:
State.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Core.Model
{
public class State
{
[Key]
public virtual int StateID { get; set; }
public virtual Int32 CountryID { get; set; }
[Display(Name = "State Name")]
public virtual string Statename
{ get; set; }
[Display(Name = "District List")]
public virtual ICollection<District> DistrictList { get; set; }
}
}
Code:
District.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Core.Model
{
public class District
{
[Key]
public virtual int DistrictID { get; set; }
public virtual int StateID { get; set; }
[Display(Name = "District Name")]
public virtual string DistrictName
{ get; set; }
[Display(Name = "Tehsil List")]
public virtual ICollection<Tehsil> TehsilList { get; set; }
}
}
Code:
Tehsil.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Core.Model
{
public class Tehsil
{
[Key]
public virtual int TehsilID { get; set; }
public virtual int DistrictID { get; set; }
[Display(Name = "Tehsil Name")]
public virtual string TehsilName
{ get; set; }
}
}
Code:
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using ResourcesLibrary.Properties;
using DataAnnotationsExtensions;
using Core.Utility;
namespace Core.Model
{
public class Student
{
//[Key]
public virtual int Id { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "NameRequired")]
public virtual string Name { get; set; }
public virtual int Age { get; set; }
//[Required]
[DateStartAttribute(ErrorMessage="Provide Other Date ")]
[Display (Name="Date of Birth")]
public virtual DateTime Dob { get; set; }
[Display(Name = "Country")]
public virtual Int32 CountryID { get; set; }
[Display(Name = "State")]
public virtual int StateID { get; set; }
public virtual int DistrictID { get; set; }
public virtual int TehsilID { get; set; }
[Required]
public virtual string Address { get; set; }
[Email]
[Required]
public virtual string Email { get; set; }
}
}
Code:
Country.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Core.Model.Country">
<id name="CountryID" type="Int32">
<generator class="native"/>
</id>
<property name="Countryname" />
<set name="StateList" cascade="all">
<key column="CountryID" foreign-key="fk_CountryID"/>
<one-to-many class="Core.Model.State"/>
</set>
</class>
<query name="ListAllCountries">
from Country
</query>
</hibernate-mapping>
Code:
State.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Core.Model.State">
<id name="StateID">
<generator class="native" />
</id>
<property name="Statename" />
<set name="DistrictList" cascade="all">
<key column="StateID" foreign-key="fk_StateID"/>
<one-to-many class="Core.Model.District"/>
</set>
</class>
<query name="ListAllStates">
from State
</query>
<query name="GetStatesByCountry">
from State where CountryID =:countryID
</query>
</hibernate-mapping>
Code:
District.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Core.Model.District">
<id name="DistrictID">
<generator class="native" />
</id>
<property name="DistrictName" />
<set name="TehsilList" cascade="all">
<key column="DistrictID" foreign-key="fk_DistrictID"/>
<one-to-many class="Core.Model.Tehsil"/>
</set>
</class>
<query name="ListAllDistricts">
from District
</query>
<query name="GetDistrictsByState">
from District where StateID=:stateID
</query>
</hibernate-mapping>
Code:
Tehsil.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Core.Model.Tehsil">
<id name="TehsilID">
<generator class="native" />
</id>
<property name="TehsilName" />
</class>
<query name="ListAllTehsils">
from Tehsil
</query>
<query name="GetTehsilsByDistrict">
from Tehsil where DistrictID = :districtID
</query>
</hibernate-mapping>
Code:
Student.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="Core" namespace ="Core.Model" >
<class name ="Student" >
<id name="Id">
<generator class="native" />
</id>
<property name="Name" />
<property name="Age" />
<property name="Dob" />
<many-to-one name="CountryID" class="Core.Model.Country" column="CountryID" foreign-key="fk_Student_CountryID"/>
<many-to-one name="StateID" class="Core.Model.State" column="StateID" foreign-key="fk_Student_StateID" />
<many-to-one name="DistrictID" class="Core.Model.District" column="DistrictID" foreign-key="fk_Student_DistrictID" />
<many-to-one name="TehsilID" class="Core.Model.Tehsil" column="TehsilID" foreign-key="fk_Student_TehsilID" />
<property name="Address" />
<property name="Email" />
</class>
</hibernate-mapping>
By above code you will get idea about my Database relationship..when i am trying to insert data using saveorupdate() of NHibernate i am getting exception....like
NHibernate:Exception occurred getter of Core.Model.Country.CountryID ({"Object does not match target type."})help me plz?