简体   繁体   English

使用NHibernate和存储过程的SqlDateTime溢出

[英]SqlDateTime overflow using NHibernate and stored procedure

I'm new to NHibirnate. 我是NHibirnate的新手。 I've create mapping files, model and that work's. 我已经创建了映射文件,模型,并且可以正常工作。 But now I'm trying to use stored procedure for insert, where I'm using current date and it is throws an exception. 但是现在我正在尝试使用存储过程进行插入,而我正在使用当前日期,并且它会引发异常。

My maping: 我的地图:

  <class name="MyClass" table="table_name">
    <id name="Id" column="id">
      <generator class="identity"/>
    </id>
    <property name="Sum" column="sum"/>
    <property name="Direction" column="direction"/>
    <property name="Date" column="datetime"/>
    <property name="Number" column="number"/>
    <sql-insert xml:space="preserve">
      EXECUTE dbo.pr_AddNew @sum = ?, @direction = ?
    </sql-insert>
  </class>

My Class: 我的课:

public class MyClass
{
    public virtual int Id { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual decimal Sum { get; set; }
    public virtual byte Direction { get; set; }
    public virtual int Number { get; set; }
}

And my stored procedure: 而我的存储过程:

CREATE PROCEDURE pr_AddNew
    @sum decimal(19, 2),
    @direction int
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.table_name([datetime], sum, direction, number)
        VALUES(GETDATE(), @sum, @direction, dbo.fn_GetNextNumber())
END
GO

As you can see I'm using GETDATE() function to get current day on db side. 如您所见,我正在使用GETDATE()函数获取数据库日期。

And here is my Test method: 这是我的测试方法:

    [TestMethod]
    public void AddTest()
    {
        var myClass= new MyClass{ Direction=0, Sum=150};
        IRepository repository = new MyClassRepository();
        repository.Add(myClass);

        using (ISession session = _sessionFactory.OpenSession())
        {
            var fromDb = session.Get<MyClass>(myClass.Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(myClass, fromDb);
        }
    }

This code gives me an exception: "SqlDateTime overflow." 这段代码给了我一个例外:“ SqlDateTime溢出”。

So If I create myClass instance as: 因此,如果我将myClass实例创建为:

var myClass= new MyClass{ Direction=0, Sum=150, Date=DateTime.Now()};

everything is ok. 一切都好。 As I understand the problem is in the difference between min possible DateTime value in SQL and C#. 据我了解,问题在于SQL和C#中的最小可能DateTime值之间的差异。 But I don't need to pass datetime value to sql. 但是我不需要将datetime值传递给sql。 What can I do with this? 我该怎么办?

You have to map it as a generated column. 您必须将其映射为生成的列。 It tells NH to not send it to the database for insert or update and also tells NH that the values is loaded after insert or update (which may be a performance problem). 它告诉NH不要将其发送到数据库中以进行插入或更新,还告诉NH在插入或更新之后会加载值(这可能是性能问题)。

generated="never|insert|always"

In your case: 在您的情况下:

<property name="Date" column="datetime" generated="insert"/>

By the way, there is a completely different approach to get the same. 顺便说一下,有一种完全不同的方法来获得相同的结果。 You can set the Date in the constructor of the class and map it normally. 您可以在类的构造函数中设置Date并正常映射它。 It's much less painful in contrast to cope with stored procedures. 与应付存储过程相比,它的痛苦要小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM