FluentNHibernate:如何将数据库字符映射到 C# bool?

发布于 2024-12-04 18:17:37 字数 1194 浏览 3 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-12-11 18:17:37

对于第 1 部分,请在您的 app.config/nhibernate.config/whateverYourNHibernateConfigFile 上使用此

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

注释后编辑:

如果您使用流畅的 nhibernate,只需使用其 API,例如

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

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);
倾城°AllureLove 2024-12-11 18:17:37

对于第 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文