Database FluentNHibernate:如何将数据库字符映射到c#bool?
我已经使用FluentNHibernate建立了我的第一个项目。当我试图从单个表中获取记录列表时,我遇到了一个异常,它说: System.FormatException:字符串未被识别为有效的布尔值 问题在于,在数据库表中,有一个名为“flag”的列,其数据类型为char,但它只包含“0”或“1”的值。因此,我想将其映射到我的POCO中的bool类型:Database FluentNHibernate:如何将数据库字符映射到c#bool?,database,string,nhibernate,fluent-nhibernate,mapping,Database,String,Nhibernate,Fluent Nhibernate,Mapping,我已经使用FluentNHibernate建立了我的第一个项目。当我试图从单个表中获取记录列表时,我遇到了一个异常,它说: System.FormatException:字符串未被识别为有效的布尔值 问题在于,在数据库表中,有一个名为“flag”的列,其数据类型为char,但它只包含“0”或“1”的值。因此,我想将其映射到我的POCO中的bool类型: public class Students { public virtual int Id {get; private set;}
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.
}
}
public class StudentMap:ClassMap{
公共学生地图(){
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);//这不起作用,因为
//“flag”列是char,
//而x旗是布尔。
}
}
这是问题1
另外,在数据库中,表名是“students”,但在我的业务模型中,我想使用单数asStudent,我该怎么做?现在,如果我将我的商业类定义为Student,我将得到一个错误,它表示找不到类似表“Student”的内容
数据库是MySQL,如果这很重要的话
感谢您的提示。对于第1部分,在您的配置对象中,您需要将query.substitutions属性设置为true=1,false=0
对于第2部分,应该有一个Table(“”)方法,可以用来在StudentMap类中指定表名 对于第1部分,在配置对象中,需要将query.substitutions属性设置为true=1,false=0
对于第2部分,应该有一个Table(“”)方法,可以用来在StudentMap类中指定表名 对于第1部分,请在app.config/nhibernate.config/whateverYourNHibernateConfigFile上使用此选项
<property name="query.substitutions">true 1, false 0</property>
true 1,false 0
评论后编辑:
如果您使用的是fluent nhibernate,只需使用它的API,例如
var props = new Dictionary<string, string>();
props.Add("query.substitutions","true 1, false 0");
var sessionFactory = Fluently.Configure().BuildConfiguration().AddProperties(props);
var props=newdictionary();
添加(“查询.替换”,“真1,假0”);
var sessionFactory=fluntly.Configure().BuildConfiguration().AddProperties(props);
对于第1部分,请在app.config/nhibernate.config/whateverYourNHibernateConfigFile上使用此选项
<property name="query.substitutions">true 1, false 0</property>
true 1,false 0
评论后编辑:
如果您使用的是fluent nhibernate,只需使用它的API,例如
var props = new Dictionary<string, string>();
props.Add("query.substitutions","true 1, false 0");
var sessionFactory = Fluently.Configure().BuildConfiguration().AddProperties(props);
var props=newdictionary();
添加(“查询.替换”,“真1,假0”);
var sessionFactory=fluntly.Configure().BuildConfiguration().AddProperties(props);
谢谢您的提示。举例会更好。我已经理解了你对第二部分的暗示。对于那些寻找相同信息的人,只需在映射类中添加对Table方法的调用。e、 g.:public class StudentMap:ClassMap{public StudentMap(){Table(“students”);Id(x=>x.Id);…}
但是,我无法理解您对第1部分的提示。我在谷歌上搜索了一下,但没有找到任何有用的东西。谢谢你的提示。举例会更好。我已经理解了你对第二部分的暗示。对于那些寻找相同信息的人,只需在映射类中添加对Table方法的调用。e、 g.:public class StudentMap:ClassMap{public StudentMap(){Table(“students”);Id(x=>x.Id);…}
但是,我无法理解您对第1部分的提示。我在谷歌上搜索了一下,但没有找到任何有用的东西。好吧,这是关于FluentNHibernate的,没有XML这样的东西。我确实找到了属性节点信息,如您为NHibernate XML配置显示的信息。编辑以使用flunt nhibernateWell添加此配置,这是关于FluentNHibernate的,没有XML这样的东西。我确实找到了属性节点信息,就像您为NHibernate XML配置显示的一样