FluentNHibernate:如何映射数据库字符到C#布尔? [英] FluentNHibernate: How to map database char to c# bool?

查看:136
本文介绍了FluentNHibernate:如何映射数据库字符到C#布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用FluentNHibernate建立了我的第一个项目。当我试图从单个表中获取记录的列表时,我得到一个异常,说:

System.FormatException:字符串不被识别为有效的布尔值。 / p>

问题是在数据库表中有一个名为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); //这是行不通的,因为
//列标志是char,
//而x.Flag是bool。





这就是问题1.



另外,在数据库中,表名是students,但在我的商业模式中,我想用单数形式作为 Student 我这样做?现在,如果我将商业类定义为Student,那么会得到一个类似于表student的错误。

数据库是MySQL,如果是这样的话。



感谢您的提示。 第1部分,在你的app.config / nhibernate.config / whateverYourNHibernateConfigFile上使用这个

 < property name =query.substitutions > true 1,false 0< / property> 

编辑之后评论:

'使用流利的nhibernate,只是使用它的API,例如

  var props = new词典< string,string>(); 
props.Add(query.substitutions,true 1,false 0);
var sessionFactory = Fluently.Configure()。BuildConfiguration()。AddProperties(props);


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.

解决方案

For part 1, use this on your app.config/nhibernate.config/whateverYourNHibernateConfigFile

<property name="query.substitutions">true 1, false 0</property>

Edit after comment:

If you're using fluent nhibernate, just uses its API, example

            var props = new Dictionary<string, string>();
            props.Add("query.substitutions","true 1, false 0");
            var sessionFactory = Fluently.Configure().BuildConfiguration().AddProperties(props);

这篇关于FluentNHibernate:如何映射数据库字符到C#布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆