问题
I have found a dozen of question similar to mine but none of them offered a solution to my problem.
Thank you in advance
Ok,
I have this class
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()
{ }
}
this is the mapping....
<?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>
and this is how i am calling it
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();
}
}
}
Error... {"could not resolve property: Email of: Domain.User.User"}
StackTrace...
at NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName)
at NHibernate.Persister.Entity.AbstractEntityPersister.GetSubclassPropertyTableNumber(String propertyPath)
at NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName)
at NHibernate.Persister.Entity.AbstractEntityPersister.ToColumns(String alias, String propertyName)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(ICriteria subcriteria, String propertyName)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria, String propertyName)
at NHibernate.Criterion.CriterionUtil.GetColumnNamesUsingPropertyName(ICriteriaQuery criteriaQuery, ICriteria criteria, String propertyName, Object value, ICriterion critertion)
at NHibernate.Criterion.SimpleExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary
2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary
2 enabledFilters)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.UniqueResultT
at NHibernate.Criterion.QueryOver1.SingleOrDefault()
at NHibernate.Criterion.QueryOver
1.NHibernate.IQueryOver.SingleOrDefault()
at DataObjects.NHibernate.UserRepository.FindByEmail(String emailAddress) in E:\Projects\DataObjects.NHibernate\UserRepository.cs:line 26
at Castle.Proxies.Invocations.IUserRepository_FindByEmail.InvokeMethodOnTarget()
at 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:line 137
EDIT:
OK. Solved to some extent. I changed all my properties and components name to capital in my mapping. Instead of...
<property name="email" column="emailaddress" />
set it to...
<property name="Email" column="emailaddress" />
and it works. Now, is that a guarantee that NHibernate is populating/reading my properties via the fields? I hope so.
回答1:
This should help: Change the settings to be different for get and set
<hibernate-mapping ... default-access="field.camelcase">
and map the properties:
<property name="Email" column="emailaddress" />
NHibernate will use field for set and the Property for get. This QueryOver:
return tx.QueryOver<User>()
.Where(u => u.Email == emailAddress)
.SingleOrDefault();
...will work now
回答2:
I believe the "name" of the properties in your mapping should match the case of the public virtual properties in your class. Try changing <property name="email"...
to <property name="Email"...
and so on.
来源:https://stackoverflow.com/questions/13943908/nhibernate-could-not-resolve-property-queryover-only-one-table