NHibernate nvarchar/ntext 截断问题

发布于 2024-07-07 01:17:44 字数 815 浏览 9 评论 0原文

我使用 nhibernate 在 SQL Server Compact Edition 表中存储应用程序的一些用户设置。

这是映射文件的摘录:

<property name="Name" type="string" />
<property name="Value" type="string" />

名称是常规字符串/nvarchar(50),值在数据库中设置为 ntext

我正在尝试将大量 xml 写入“Value”属性。 我每次都会遇到异常:

@p1 : String truncation: max=4000, len=35287, value='<lots of xml..../>'

我用谷歌搜索了很多,并尝试了许多不同的映射配置:

<property name="Name" type="string" />
<property name="Value" type="string" >
  <column name="Value" sql-type="StringClob" />
</property>

这就是一个例子。 其他配置包括“ntext”而不是“StringClob”。 那些不抛出映射异常的配置仍然会抛出字符串截断异常。

这是 SQL CE 的问题(“功能”)吗? 是否可以使用 nhibernate 将超过 4000 个字符放入 SQL CE 数据库中? 如果是这样,谁能告诉我怎么做?

非常感谢!

I'm using nhibernate to store some user settings for an app in a SQL Server Compact Edition table.

This is an excerpt the mapping file:

<property name="Name" type="string" />
<property name="Value" type="string" />

Name is a regular string/nvarchar(50), and Value is set as ntext in the DB

I'm trying to write a large amount of xml to the "Value" property. I get an exception every time:

@p1 : String truncation: max=4000, len=35287, value='<lots of xml..../>'

I've googled it quite a bit, and tried a number of different mapping configurations:

<property name="Name" type="string" />
<property name="Value" type="string" >
  <column name="Value" sql-type="StringClob" />
</property>

That's one example. Other configurations include "ntext" instead of "StringClob". Those configurations that don't throw mapping exceptions still throw the string truncation exception.

Is this a problem ("feature") with SQL CE? Is it possible to put more than 4000 characters into a SQL CE database with nhibernate? If so, can anyone tell me how?

Many thanks!

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

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

发布评论

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

评论(7

水染的天色ゝ 2024-07-14 01:17:44
<property name="Value" type="string" />
  <column name="Value" sql-type="StringClob" />
</property>

我假设这是一个小错字,因为您已经关闭了属性标签两次。 只是指出这一点,以防万一它不是一个错字。

<property name="Value" type="string" />
  <column name="Value" sql-type="StringClob" />
</property>

I'm assuming this is a small typo, since you've closed the property tag twice. Just pointing this out, in case it wasn't a typo.

风铃鹿 2024-07-14 01:17:44

尝试

Try <property name="Value" type="string" length="4001" />

总以为 2024-07-14 01:17:44

尝试过:

<property name="Value" type="string" length="4001" />

恐怕

<property name="Value" type="string" >
  <column name="Value" sql-type="StringClob" length="5000"/>
</property>

都不起作用...同样的例外 - 它仍然说最大值是 4000。

Tried:

<property name="Value" type="string" length="4001" />

and

<property name="Value" type="string" >
  <column name="Value" sql-type="StringClob" length="5000"/>
</property>

Neither worked, I'm afraid... Same exception - it still says that the max value is 4000.

念﹏祤嫣 2024-07-14 01:17:44

为什么使用子元素语法?

尝试:

<property name='Value' type='StringClob' />

Why are you using the sub-element syntax?

try:

<property name='Value' type='StringClob' />
年华零落成诗 2024-07-14 01:17:44

在我当前的 SQL CE 和 NHibernate 部署中,我使用的长度为 4001。然后 NHibernate 将这些内容生成为 NTEXT 而不是 NVARCHAR。

尝试一下。

与 NHibernate 和 SQL CE 一起使用的另一件事是:

<session-factory>
  ...
  <property name="connection.release_mode">on_close</property>
</session-factory>

这至少为我解决了一些其他问题。

On my current deplyoment of SQL CE and NHibernate I use a length of 4001. Then NHibernate generates the stuff as NTEXT instead of NVARCHAR.

Try that.

Another thing to use with NHibernate and SQL CE is:

<session-factory>
  ...
  <property name="connection.release_mode">on_close</property>
</session-factory>

That solves some other problems for me atleast.

浅忆流年 2024-07-14 01:17:44

好的,非常感谢此帖子中的 Artur,解决方案如下:
用一个新的驱动程序从 SqlServerCeDriver 继承,并重写 InitializeParamter 方法:

using System.Data;
using System.Data.SqlServerCe;
using NHibernate.Driver;
using NHibernate.SqlTypes;

namespace MySqlServerCeDriverNamespace
{
    /// <summary>
    /// Overridden Nhibernate SQL CE Driver,
    /// so that ntext fields are not truncated at 4000 characters
    /// </summary>
    public class MySqlServerCeDriver : SqlServerCeDriver
    {
        protected override void InitializeParameter(
            IDbDataParameter dbParam,
            string name,
            SqlType sqlType)
        {
            base.InitializeParameter(dbParam, name, sqlType);

            if (sqlType is StringClobSqlType)
            {
                var parameter = (SqlCeParameter)dbParam;
                parameter.SqlDbType = SqlDbType.NText;
            }

        }
    }
}

然后,在你的 app.config 中使用这个驱动程序而不是 NHibernate 的驱动程序。

<nhibernateDriver>MySqlServerCeDriverNamespace.MySqlServerCeDriver , MySqlServerCeDriverNamespace</nhibernateDriver>

我看到很多其他帖子中人们都遇到了这个问题,并通过更改 sql- 来解决它type 属性为“StringClob” - 正如本线程中所尝试的那样。

我不确定为什么它对我不起作用,但我怀疑这是因为我使用的是 SQL CE 而不是其他数据库。 但是你现在有了!

Okay, with many thanks to Artur in this thread, here's the solution:
Inherit from the SqlServerCeDriver with a new one, and override the InitializeParamter method:

using System.Data;
using System.Data.SqlServerCe;
using NHibernate.Driver;
using NHibernate.SqlTypes;

namespace MySqlServerCeDriverNamespace
{
    /// <summary>
    /// Overridden Nhibernate SQL CE Driver,
    /// so that ntext fields are not truncated at 4000 characters
    /// </summary>
    public class MySqlServerCeDriver : SqlServerCeDriver
    {
        protected override void InitializeParameter(
            IDbDataParameter dbParam,
            string name,
            SqlType sqlType)
        {
            base.InitializeParameter(dbParam, name, sqlType);

            if (sqlType is StringClobSqlType)
            {
                var parameter = (SqlCeParameter)dbParam;
                parameter.SqlDbType = SqlDbType.NText;
            }

        }
    }
}

Then, use this driver instead of NHibernate's in your app.config

<nhibernateDriver>MySqlServerCeDriverNamespace.MySqlServerCeDriver , MySqlServerCeDriverNamespace</nhibernateDriver>

I saw a lot of other posts where people had this problem, and solved it by just changing the sql-type attribute to "StringClob" - as attempted in this thread.

I'm not sure why it wouldn't work for me, but I suspect it is the fact that I'm using SQL CE and not some other DB. But, there you have it!

罪#恶を代价 2024-07-14 01:17:44

阅读您的文章后,此修改使其在我的代码中正常工作

protected override void InitializeParameter(IDbDataParameter dbParam,string name,SqlType sqlType)
    {
        base.InitializeParameter(dbParam, name, sqlType);

        var stringType = sqlType as StringSqlType;
        if (stringType != null && stringType.LengthDefined && stringType.Length > 4000)
        {
            var parameter = (SqlCeParameter)dbParam;
            parameter.SqlDbType = SqlDbType.NText;
        }

    }

After reading your post this modification got it working in my code

protected override void InitializeParameter(IDbDataParameter dbParam,string name,SqlType sqlType)
    {
        base.InitializeParameter(dbParam, name, sqlType);

        var stringType = sqlType as StringSqlType;
        if (stringType != null && stringType.LengthDefined && stringType.Length > 4000)
        {
            var parameter = (SqlCeParameter)dbParam;
            parameter.SqlDbType = SqlDbType.NText;
        }

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