流畅的nhibernate问题映射char(1)类型

发布于 2024-08-05 06:50:53 字数 1711 浏览 7 评论 0原文

地图。

    public SocialCodeMap()
    {
        Id(x => x.SocialCodeId);
        Map(x => x.Name);
        Map(x => x.Code);
        Map(x => x.DisplayOrder);
    }

还有班级。

public class SocialCode
{
    public virtual Guid SocialCodeId { get; set; }
    public virtual string Name { get; set; }
    public virtual char Code { get; set; }
    public virtual int DisplayOrder { get; set; }
}

还有电话。

    public SocialCode FetchByCode(char code)
    {
        return Session.CreateCriteria<SocialCode>().Add<SocialCode>(x => x.Code == code).UniqueResult<SocialCode>();
    }

运行 FetchByCode() 时收到此错误。

System.Exception: Cannot convert '65' to System.Char
at NHibernate.LambdaExtensions.ExpressionProcessor.ConvertType(Object value, Type type)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessExpression<T>(Expression`1 expression)
at NHibernate.LambdaExtensions.ICriteriaExtensions.Add<T>(ICriteria criteria, Expression`1 expression)
at DAL.NHibernate.xxx.SocialCodeRepository.FetchByCode(Char code) in SocialCodeRepository.cs: line 18
at UnitTests.DAL.xxx.yyy.SocialCodeRepositoryFixture.FetchByCode_ReturnsNullCodeDNE() in SocialCodeRepositoryFixture.cs: line 38 

似乎 NHibernate 正在将我的 char 转换为 int 。如何强制它使用列上的 char(1) 类型?

The map.

    public SocialCodeMap()
    {
        Id(x => x.SocialCodeId);
        Map(x => x.Name);
        Map(x => x.Code);
        Map(x => x.DisplayOrder);
    }

And the Class.

public class SocialCode
{
    public virtual Guid SocialCodeId { get; set; }
    public virtual string Name { get; set; }
    public virtual char Code { get; set; }
    public virtual int DisplayOrder { get; set; }
}

And the call.

    public SocialCode FetchByCode(char code)
    {
        return Session.CreateCriteria<SocialCode>().Add<SocialCode>(x => x.Code == code).UniqueResult<SocialCode>();
    }

I receive this error when running FetchByCode().

System.Exception: Cannot convert '65' to System.Char
at NHibernate.LambdaExtensions.ExpressionProcessor.ConvertType(Object value, Type type)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessExpression<T>(Expression`1 expression)
at NHibernate.LambdaExtensions.ICriteriaExtensions.Add<T>(ICriteria criteria, Expression`1 expression)
at DAL.NHibernate.xxx.SocialCodeRepository.FetchByCode(Char code) in SocialCodeRepository.cs: line 18
at UnitTests.DAL.xxx.yyy.SocialCodeRepositoryFixture.FetchByCode_ReturnsNullCodeDNE() in SocialCodeRepositoryFixture.cs: line 38 

It seems NHibernate is convering my char to an int somewhere along the way. How can I force it to use the char(1) type that is on the column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

櫻之舞 2024-08-12 06:50:53

我假设 Fluent-NH 可以自动正确检测字符类型。但如果不能,您可以随时向其显示提示。尝试此

Map(x => x.Code).CustomType<NHibernate.Type.CharType>();

更新:

抱歉,我还没有测试代码。我尝试了一下,发现错误出在 NH Lambda Extensions 中。简而言之,

// mapping
Map(x => x.Code);

// querying
criteria.Add(Restrictions.Eq("Code", 'A'));

我测试过这应该可行。

I assume Fluent-NH can automatically detect char type correctly. But if it can't, you can always show a hint to it. Try this

Map(x => x.Code).CustomType<NHibernate.Type.CharType>();

Update:

Sorry that I haven't tested the code. Well I tried and found that the bug is in NH Lambda Extensions. In short,

// mapping
Map(x => x.Code);

// querying
criteria.Add(Restrictions.Eq("Code", 'A'));

I tested this should work.

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