5

我发现了十几个与我类似的问题,但没有一个能解决我的问题。

先感谢您

行,

我有这门课

public class User : IEntity
    {
        private int id;
        public virtual int Id { get { return id; } }


        private string email;

        public virtual string Email
        {
            get { return email; }
            //private set { email = value; }
        }

        private string password;

        public virtual string Password
        {
            get { return password; }
            //private set { password = value; }
        }

        private bool isActive;

        public virtual bool IsActive
        {
            get { return isActive; }
            //private set { isActive = value; }
        }

        private bool isRegistered;

        public virtual bool IsRegistered
        {
            get { return isRegistered; }
            //private set { isRegistered = value; }
        }

        private bool hasRequestedApplication;

        public virtual bool HasRequestedApplication
        {
            get { return hasRequestedApplication; }
            //private set { hasRequestedApplication = value; }
        }


        private ContactInfo contactInformation;

        public virtual ContactInfo ContactInformation
        {
            get { return contactInformation; }
            //private set { contactInformation = value; }
        }



        public User(string email)
        {
            this.email = email;
        }

        public User(string email, string password):this(email)
        {
            this.password = password;
        }

        public User()
        { }
}

这是映射....

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain"
                 namespace="Domain.User" default-access="field">

  <class name="User" table="[User]">

    <id name="id" column="UserID">
      <generator class="identity" />
    </id>
    <property name="email" column="Email" not-null="true"></property>
    <property name="password" column="HashedPassword" not-null="false"></property>
    <property name="isRegistered" column="IsRegistered" not-null="true"></property>
    <property name="isActive" column="IsActive" not-null="true"></property>
    <property name="hasRequestedApplication" column="HasRequestedApplication" not-null="true"></property>

    <one-to-one name="contactInformation" class="Domain.User.ContactInfo"/>
  </class>
</hibernate-mapping>

这就是我所说的

 public class UserRepository: IUserRepository
    {
        Func<ISession> session;

        public UserRepository(Func<ISession> _session)
        {
            session = _session;
        }

        [Transaction]
        public User FindByEmail(string emailAddress)
        {
            using (var tx = session())
            {
                return tx.QueryOver<User>().Where(u => u.Email == emailAddress).SingleOrDefault();
            }
        }
}

错误... {“无法解析属性:电子邮件地址:Domain.User.User”}

堆栈跟踪...

NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName) 在 NHibernate.Persister NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName) NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection (ICriteria subcriteria, String propertyName) at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(ICriteria subcriteria, String propertyName) at NHibernate.Criterion NHibernate.Criterion 的 .CriterionUtil.GetColumnNamesUsingPropertyName(ICriteriaQuery criteriaQuery, ICriteria criteria, String propertyName, Object value, ICriterion critertion)。SimpleExpression.ToSqlString(ICriteria 条件,ICriteriaQuery 条件查询,IDictionary2 enabledFilters) at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary2 enabledFilters) 在 NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable 持久化器,CriteriaQueryTranslator 转换器,ISessionFactoryImplementor 工厂,ICriteria 标准,字符串 rootEntityName,IDictionary 2 enabledFilters) at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary2 enabledFilters) 在 NHibernate.Impl.SessionImpl.List(CriteriaImpl 标准,IList 结果) 在NHibernate.Impl.CriteriaImpl.UniqueResultT 在 NHibernate.Impl.CriteriaImpl.List(IList 结果) 在 NHibernate.Criterion.QueryOver1.SingleOrDefault() at NHibernate.Criterion.QueryOver1.NHibernate.IQueryOver.SingleOrDefault() at DataObjects.NHibernate.UserRepository.FindByEmail(String emailAddress) 在 E:\Projects\DataObjects.NHibernate\UserRepository.cs:第 26 行 Castle.Proxies.Invocations.IUserRepository_FindByEmail.InvokeMethodOnTarget() 在Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Facilities.AutoTx.TransactionInterceptor.SynchronizedCase(IInvocation invocation, ITransaction transaction) in d:\BuildAgent-03\work\9844bdf039249947\src\Castle.Facilities.AutoTx\TransactionInterceptor.cs:第 137 行

编辑:

好的。一定程度上解决了。我在映射中将所有属性和组件名称更改为大写。代替...

<property name="email" column="emailaddress" />

将其设置为...

<property name="Email" column="emailaddress" />

它有效。现在,这是 NHibernate 通过字段填充/读取我的属性的保证吗?但愿如此。

4

2 回答 2

3

这应该会有所帮助:将设置更改为不同的getset

<hibernate-mapping ... default-access="field.camelcase">

并映射属性:

<property name="Email" column="emailaddress" />

NHibernate 将使用字段进行设置,使用属性进行获取。这个查询结束:

return tx.QueryOver<User>()    
  .Where(u => u.Email == emailAddress)  
  .SingleOrDefault();

...现在可以工作了

于 2012-12-19T07:04:04.940 回答
2

我相信映射中属性的“名称”应该与类中公共虚拟属性的大小写相匹配。尝试更改<property name="email"...<property name="Email"...等等。

于 2012-12-19T00:53:03.843 回答