我有以下持久化类和相应的XML映射文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer.Poco
{
public class User
{
private int id;
private int positionId;
private int companyId;
private int roleId;
private int languageId;
private string firstname;
private string firstnameUpperCase;
private string lastname;
private string initials;
private string pseudonyms;
private string mail;
private string phone;
private string mobile;
private bool managementFlag;
private bool activityFlag;
private string information;
private string image;
private string path;
public User()
{
}
public virtual int Id
{
get { return id; }
set { id = value; Console.WriteLine("setter Id");}
}
public virtual int PositionId
{
get { return positionId; }
set { positionId = value; Console.WriteLine("setter PositionId"); }
}
public virtual int CompanyId
{
get { return companyId; }
set { companyId = value; Console.WriteLine("setter CompanyId"); }
}
public virtual int RoleId
{
get { return roleId; }
set { roleId = value; Console.WriteLine("setter RoleId"); }
}
public virtual int LanguageId
{
get { return languageId; }
set { languageId = value; Console.WriteLine("setter LanguageId"); }
}
public virtual string Firstname
{
get { return firstname; }
set { firstname = value; Console.WriteLine("setter Firstname"); }
}
public virtual string FirstnameUpperCase
{
get { return firstnameUpperCase; }
set { firstnameUpperCase = value; Console.WriteLine("setter FirstnameUpperCase"); }
}
public virtual string Lastname
{
get { return lastname; }
set { lastname = value; Console.WriteLine("setter Lastname"); }
}
public virtual string Initials
{
get { return initials; }
set { initials = value; Console.WriteLine("setter Initials"); }
}
public virtual string Pseudonyms
{
get { return pseudonyms; }
set { pseudonyms = value; Console.WriteLine("setter Pseudonyms"); }
}
public virtual string Mail
{
get { return mail; }
set { mail = value; Console.WriteLine("setter Mail"); }
}
public virtual string Phone
{
get { return phone; }
set { phone = value; Console.WriteLine("setter Phone"); }
}
public virtual string Mobile
{
get { return mobile; }
set { mobile = value; Console.WriteLine("setter Mobile"); }
}
public virtual bool ManagementFlag
{
get { return managementFlag; }
set { managementFlag = value; Console.WriteLine("setter ManagementFlag"); }
}
public virtual bool ActivityFlag
{
get { return activityFlag; }
set { activityFlag = value; Console.WriteLine("setter activityFlag"); }
}
public virtual string Information
{
get { return information; }
set { information = value; Console.WriteLine("setter information"); }
}
public virtual string Image
{
get { return image; }
set { image = value; Console.WriteLine("setter image"); }
}
public virtual string Path
{
get { return path; }
set { path = value; Console.WriteLine("setter path"); }
}
}
}
和XML文件:
<many-to-one name="CompanyId"
unique="true"
class="Company">
<column name="companyId" />
</many-to-one>
<many-to-one name="PositionId"
unique="true"
class="Position">
<column name="positionId" />
</many-to-one>
<many-to-one name="RoleId"
unique="true"
class="Role">
<column name="roleId" />
</many-to-one>
<many-to-one name="LanguageId"
unique="true"
class="Language">
<column name="languageId" />
</many-to-one>
<property name="Firstname" column="firstname" type="String" />
<property name="FirstnameUpperCase" column="firstnameUpperCase" type="String" />
<property name="Lastname" column="lastname" type="String" />
<property name="Initials" column="initials" type="String" />
<property name="Pseudonyms" column="pseudonyms" type="String" />
<property name="Mail" column="mail" type="String" />
<property name="Phone" column="phone" type="String" />
<property name="Mobile" column="mobile" type="String" />
<property name="ManagementFlag" column="managementFlag" type="Boolean" />
<property name="ActivityFlag" column="activityFlag" type="Boolean" />
<property name="Information" column="information" type="String" />
<property name="Image" column="image" type="String" />
<property name="Path" column="path" type="String" />
每次,我想从MySQL数据库中读取一个User对象,我得到以下异常:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of DataAccessLayer.Poco.User ---> System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
bei (Object , Object[] , SetterCallback )
bei NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values)
bei NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
--- Ende der internen Ausnahmestapelüberwachung ---
bei NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
bei NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values)
bei NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode)
bei NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)
bei NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly)
bei NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
bei NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
bei NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer)
bei NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
bei NHibernate.Loader.Loader.List(ISessionImplementor'test.exe' (CLR v4.0.30319: test.exe):
session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
bei NHibernate.Loader.Hql.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
bei NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
bei NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
bei NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
bei NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
bei NHibernate.Impl.AbstractQueryImpl2.List()
bei DataAccessLayer.Impl.UserDao.get(Int32 entityId)
答案 0 :(得分:2)
最显而易见的是,您需要再次查看many-to-one element。多对一元素的使用方式如下:
public class Foo {
public Bar RelatedBar {get; set;}
}
public class Bar {
public int BarId {get; set;}
}
然后可以像这样映射 Foo
:
<many-to-one name="RelatedBar" class="Bar"><!-- class attribute is optional -->
<column name="RelatedBarId" />
</many-to-one>
如果你真的想在Foo类中使用像RelatedBarId这样的东西,你需要使用property-element映射它,然后NHibernate就不会知道这种关系了。