我正在 MySql 上使用 Castle Active Record 和 NHibernate 实现一个相对简单的用户管理模型,我遇到了一个问题。
我们说,我有两个表_ 用户和_ 密码由以下SQL创建语句描述
CREATE TABLE _users ( id bigint(20) NOT NULL AUTO_INCREMENT, username char(32) NOT NULL, PRIMARY KEY (id), UNIQUE KEY username_UQ (username), ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE _passwords ( id bigint(20) NOT NULL AUTO_INCREMENT, creation_date datetime NOT NULL, user_id bigint(20) NOT NULL, password_hash char(64) NOT NULL, valid_end_date datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY user_id_password_UQ (user_id,password_hash), KEY user_passwords_FK (user_id), CONSTRAINT user_passwords_FK FOREIGN KEY (user_id) REFERENCES _users (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这个想法是保留一个原始密码历史记录,因此,密码保存在一个单独的表_ 密码中,它与_ 用户具有多对一的关系 table。
现在,以下C#代码使用Castle Active Records
namespace DataEntities
{
[ActiveRecord("_users")]
public class User : ActiveRecordBase<User>
{
[PrimaryKey(PrimaryKeyType.Identity, "id", Access = PropertyAccess.NosetterLowercase)]
public ulong Id
{
get;
set;
} // Id
[Property("username", ColumnType = "String", NotNull = true, Unique = true)]
[ValidateIsUnique]
public string Username
{
get;
set;
} // Username
[HasMany(typeof(Password))]
public IList<Password> Passwords
{
get;
set;
} // Passwords
public string ValidPasswordHash
{
get
{
DateTime l_dtNow = DateTime.Now;
if (Passwords.Count != 1 || Passwords[0].ValidFrom >= l_dtNow || Passwords[0].ValidUntil <= l_dtNow)
{
throw new Exception();
}
return Encoding.UTF8.GetString(Passwords[0].PasswordHash);
}
} // ValidPasswordHash
public static User FindByCredentials(string i_sUsername, string i_sHashedPassword)
{
return FindOne(Restrictions.Eq("Username", i_sUsername), Restrictions.Eq("ValidPasswordHash", i_sHashedPassword));
} // FindByCredentials
} // User
[ActiveRecord("_passwords")]
public class Password : ActiveRecordBase<Password>
{
[PrimaryKey(PrimaryKeyType.Identity, "id", Access = PropertyAccess.NosetterLowercase)]
public ulong Id
{
get;
set;
} // Id
[BelongsTo("user_id", NotNull = true, UniqueKey = "_passwords_UQ1")]
public ulong UserId
{
get;
set;
} // UserId
[Property("password_hash", ColumnType = "UInt64", NotNull = true, UniqueKey = "_passwords_UQ1")]
public byte[] PasswordHash
{
get;
set;
} // PasswordHash
[Property("creation_date", ColumnType = "DateTime", NotNull = true)]
public DateTime ValidFrom
{
get;
set;
} // ValidFrom
[Property("valid_end_date", ColumnType = "DateTime", NotNull = true)]
public DateTime ValidUntil
{
get;
set;
} // ValidUntil
} // Password
} // DataEntities
并在我的应用程序启动框架初始化
try
{
ActiveRecordStarter.Initialize(ActiveRecordSectionHandler.Instance, typeof(Password),
typeof(User));
}
catch (Exception l_excpt)
{
// handle exception
}
最后,当此代码运行时,它会生成以下异常:
Castle.ActiveRecord.Framework.ActiveRecordException: ActiveRecord tried to infer details about the relation User.Passwords but it could not find a 'BelongsTo' mapped property in the target type DataEntities.Password at Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitHasMany(HasManyModel model) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\Internal\Visitors\SemanticVerifierVisitor.cs:line 544 at Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable nodes) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\Internal\Visitors\AbstractDepthFirstVisitor.cs:line 45 at Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitModel(ActiveRecordModel model) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\Internal\Visitors\AbstractDepthFirstVisitor.cs:line 59 at Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitModel(ActiveRecordModel model) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\Internal\Visitors\SemanticVerifierVisitor.cs:line 122 at Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable nodes) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\Internal\Visitors\AbstractDepthFirstVisitor.cs:line 45 at Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder holder, IConfigurationSource source, IEnumerable`1 types, Boolean ignoreProblematicTypes) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:line 927 at Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource source, Type[] types) in c:\daten\dev\External\Castle\AR2.0\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:line 202 at Global.Application_Start(Object sender, EventArgs e) in C:\Projects Code\xyz\Global.asax.cs:line 22
好吧,我已经无休止地盯着UserId
课程的Password
属性,我用Google搜索,现在我很失落。所以社区是我最后的希望......任何人都可以帮助我理解导致此异常的原因以及如何解决这个问题?
提前感谢您的回复和评论。