Fluent NHibernate 会导致“将数字转换为数字数据类型时出现算术溢出错误”插入/保存时

发布于 2024-10-19 18:02:48 字数 5443 浏览 1 评论 0原文

此代码产生“将数字转换为数字数据类型时发生算术溢出错误

Session.Save(new Keyword
{
    Approved = true,
    Custom = false,
    Value = "toto",
    Language = "en"
});

Keyword 的映射如下

public class KeywordMap : ClassMap<Keyword>
{
    public KeywordMap()
    {
        Table("PRODUCTS_Keywords");

        Id(x => x.Id, "keywordID").GeneratedBy.Identity();

        Map(x => x.Value, "keyword")
            .Not.Nullable();

        Map(x => x.Language, "language")
            .Not.Nullable();

        Map(x => x.Approved, "approved")
            .Not.Nullable();

        Map(x => x.Custom, "custom")
            .Not.Nullable();
    }
}

Keyword 的实体如下

public class Keyword
{
    public virtual Int64 Id { get; private set; }
    public virtual string Value { get; set; }
    public virtual string Language { get; set; }
    public virtual bool Approved { get; set; }
    public virtual bool Custom { get; set; }
}

这是我的数据库中表 PRODUCTS_Keyword 的表示形式(我使用 MSSQL 2008 R2)

CREATE TABLE [dbo].[PRODUCTS_Keywords](
    [keywordID] [bigint] IDENTITY(1,1) NOT NULL,
    [keyword] [nvarchar](50) NOT NULL,
    [language] [char](2) NOT NULL,
    [approved] [bit] NOT NULL,
    [custom] [bit] NOT NULL,
PRIMARY KEY CLUSTERED

这是 Keyword 的导出映射

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Keyword, Main, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null" table="PRODUCTS_Keywords">
    <id name="Id" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="keywordID" />
      <generator class="identity" />
    </id>
    <property name="Value" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="keyword" not-null="true" />
    </property>
    <property name="Language" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="language" not-null="true" />
    </property>
    <property name="Approved" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="approved" not-null="true" />
    </property>
    <property name="Custom" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="custom" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

这是堆栈跟踪

at NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch()
at NHibernate.AdoNet.AbstractBatcher.OnPreparedCommand()
at NHibernate.AdoNet.AbstractBatcher.PrepareCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
at NHibernate.AdoNet.AbstractBatcher.PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteInserts()
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)

目前,表 PRODUCTS_Keywords 中有 266 016 条记录,Max(keywordID) 为 8 942 223

问题的原因是什么?如何解决?

谢谢

This code produces an "Arithmetic overflow error converting numeric to data type numeric"

Session.Save(new Keyword
{
    Approved = true,
    Custom = false,
    Value = "toto",
    Language = "en"
});

The mapping of Keyword is the following

public class KeywordMap : ClassMap<Keyword>
{
    public KeywordMap()
    {
        Table("PRODUCTS_Keywords");

        Id(x => x.Id, "keywordID").GeneratedBy.Identity();

        Map(x => x.Value, "keyword")
            .Not.Nullable();

        Map(x => x.Language, "language")
            .Not.Nullable();

        Map(x => x.Approved, "approved")
            .Not.Nullable();

        Map(x => x.Custom, "custom")
            .Not.Nullable();
    }
}

The entity of Keyword is the following

public class Keyword
{
    public virtual Int64 Id { get; private set; }
    public virtual string Value { get; set; }
    public virtual string Language { get; set; }
    public virtual bool Approved { get; set; }
    public virtual bool Custom { get; set; }
}

This is the representation of table PRODUCTS_Keyword in my database (I use MSSQL 2008 R2)

CREATE TABLE [dbo].[PRODUCTS_Keywords](
    [keywordID] [bigint] IDENTITY(1,1) NOT NULL,
    [keyword] [nvarchar](50) NOT NULL,
    [language] [char](2) NOT NULL,
    [approved] [bit] NOT NULL,
    [custom] [bit] NOT NULL,
PRIMARY KEY CLUSTERED

This is the exported mapping for Keyword

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Keyword, Main, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null" table="PRODUCTS_Keywords">
    <id name="Id" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="keywordID" />
      <generator class="identity" />
    </id>
    <property name="Value" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="keyword" not-null="true" />
    </property>
    <property name="Language" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="language" not-null="true" />
    </property>
    <property name="Approved" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="approved" not-null="true" />
    </property>
    <property name="Custom" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="custom" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

This is the stacktrace

at NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch()
at NHibernate.AdoNet.AbstractBatcher.OnPreparedCommand()
at NHibernate.AdoNet.AbstractBatcher.PrepareCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
at NHibernate.AdoNet.AbstractBatcher.PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteInserts()
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)

Currently, there are 266 016 records in table PRODUCTS_Keywords and the Max(keywordID) is 8 942 223

What is the cause of the problem and how can I fix it ?

Thank you

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

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

发布评论

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

评论(1

定格我的天空 2024-10-26 18:02:48

您的设置唯一有问题的地方是让 set 访问器是私有的。这可能没问题,但我只见过 public 和 protected 在这些情况下使用。

你能显示 fluid 生成的 sql 语句吗?配置 Fluent 时使用 ShowSql(),它会将尝试执行的 SQL 输出到控制台。实际的错误消息似乎是 SQL 错误消息。

The only thing that seems questionable about your setup is having the set accessor be private. That might be fine, but I've only ever seen public and protected used in these situations.

Can you show the sql statement that's being generated by fluent? Use ShowSql() when configuring Fluent and it will output to the console what SQL it is attempting to execute. The actual error messages appears to be a SQL error message.

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