FluentNHibernate:如何将数据库字符映射到 C# bool?
我已经使用 FluentNHibernate 建立了我的第一个项目。当我尝试从单个表中获取记录列表时,出现异常:
System.FormatException:字符串未被识别为有效的布尔值。
问题在于,在数据库表中,有一个名为“flag”的列,其数据类型为char,但它只包含“0”或“1”值。所以,我想将其映射到 POCO 中的 bool 类型:
public class Students
{
public virtual int Id {get; private set;}
public virtual string FirstName {get; set;}
public virtual string LastName {get; set;}
public virtual DateTime RegisterDate {get; set;}
public virtual bool Flag {get; set;}
}
现在,在我的 Mappings.cs 中,如何将 Flag 转换为 bool?
public class StudentMap : ClassMap<Students> {
public StudentMap() {
Id(x => x.Id);
Map(x => x.FirstName).Column("first_name");
Map(x => x.LastName).Column("last_name");
Map(x => x.RegisterDate).Column("register_date");
Map(x => x.Flag); // This won't work because
// column "flag" is char,
// whereas x.Flag is bool.
}
}
这就是问题1。
另外,在数据库中,表名称是“students”,但在我的业务模型中,我想使用单数为Student,我该怎么做?现在,如果我将商务类定义为学生,我会收到一条错误,提示未找到“学生”表之类的内容。
如果重要的话,数据库是 MySQL。
谢谢你的提示。
I have set up my first project using FluentNHibernate. When I tried to get a list of records from a single table, I got an exception which says:
System.FormatException : String was not recognized as a valid Boolean.
The problem is that in the database table, there is a column called "flag" and its data type is char, but it only contains values of either '0' or '1'. So, I'd like to map it to type bool in my POCO:
public class Students
{
public virtual int Id {get; private set;}
public virtual string FirstName {get; set;}
public virtual string LastName {get; set;}
public virtual DateTime RegisterDate {get; set;}
public virtual bool Flag {get; set;}
}
Now, in my Mappings.cs, how do I convert Flag to bool?
public class StudentMap : ClassMap<Students> {
public StudentMap() {
Id(x => x.Id);
Map(x => x.FirstName).Column("first_name");
Map(x => x.LastName).Column("last_name");
Map(x => x.RegisterDate).Column("register_date");
Map(x => x.Flag); // This won't work because
// column "flag" is char,
// whereas x.Flag is bool.
}
}
That's question 1.
Also, in the database, the table name is "students", but in my business model, I want to use the singular as Student, how can I do this? Right now, if I define my business class as Student, I will get an error which says something like the table "student" is not found.
The database is MySQL, if that matters.
Thanks for your hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于第 1 部分,请在您的 app.config/nhibernate.config/whateverYourNHibernateConfigFile 上使用此
注释后编辑:
如果您使用流畅的 nhibernate,只需使用其 API,例如
For part 1, use this on your app.config/nhibernate.config/whateverYourNHibernateConfigFile
Edit after comment:
If you're using fluent nhibernate, just uses its API, example
对于第 1 部分,在配置对象中,您需要将 query.substitutions 属性设置为 true=1、false=0。
对于第 2 部分,应该有一个 Table("") 方法,可用于在 StudentMap 类中指定表名称。
For part 1, in your configuration object, you need to set the query.substitutions property to true=1, false=0.
For part 2, there should be a Table("") method you can use to specify the table name in the StudentMap class.