如何将字符串属性映射到数据库中的二进制列?
我有一个用户实体类。其中一个属性是用户的密码(实际上是哈希值)。我把它做成了一个字符串(简化的代码):
public class User
{
public virtual int Id { get; set; }
public virtual string Password { get; set; }
}
还有一个流畅的 NHibernate 映射(简化的代码):
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).GeneratedBy.Sequence("users_id_seq");
Map(x => x.Password); // what do I put here???
}
}
数据库列是 bytea PostgreSQL 上的数据类型。上面的映射不起作用,因为该属性是字符串(文本)。我应该怎么办?
I have a class for a user entity. One of the properties is the user's password (a hash, actually). I made it a string (streamlined code):
public class User
{
public virtual int Id { get; set; }
public virtual string Password { get; set; }
}
There's also a Fluent NHibernate mapping (streamlined code):
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).GeneratedBy.Sequence("users_id_seq");
Map(x => x.Password); // what do I put here???
}
}
The database column is of bytea data type on PostgreSQL. The above mapping doesn't work, because the property is string (text). What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
Password
设为公共属性,该属性仅用于引用底层私有属性HashedPassword
。像这样:
<代码>
protected virtual byte[] /*或任何类型*/ HashedPassword {get;放;}
公共虚拟字符串密码
获取
{
返回(字符串)(哈希密码); //或者您想将其转换为字符串...
}
设置
//...
然后您可以告诉 Fluent nHib 忽略您的密码属性。
you can Make
Password
a public property, which is only used to reference to the underlying private propertyHashedPassword
.something like so:
protected virtual byte[] /*or whatever the type is*/ HashedPassword {get; set;}
public virtual string Password
get
{
return (string)(HashedPassword); //or however you want to cast it to string...
}
set
//...
you can then tell fluent nHib to ignore your Password property.
这是双向工作(读和写)的最终解决方案,我之前尝试过的方法无法正常工作。希望这会对某人有所帮助。
Here is the final solution which works both ways (read & write), what I tried earlier didn't work properly. Hope this will help somebody.