Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate won't persist DateTime SqlDateTime overflow
    primarykey
    data
    text
    <p>I am working on an ASP.NET MVC project with NHibernate as the backend and am having some trouble getting some dates to write back to my SQL Server database tables.</p> <p>These date fields are NOT nullable, so the many answers here about how to setup nullable datetimes have not helped.</p> <p>Basically when I try to save the entity which has a DateAdded and a LastUpdated fields, I am getting a SqlDateTime overflow exception. I have had a similar problem in the past where I was trying to write a datetime field into a smalldatetime column, updating the type on the column appeared to fix the problem. My gut feeling is that its going to be some problem with the table definition or some type of incompatible data types, and the overflow exception is a bit of a bum steer.</p> <p>I have attached an example of the table definition and the query that NHibernate is trying to run, any help or suggestions would be greatly appreciated.</p> <pre><code>CREATE TABLE [dbo].[CustomPages]( [ID] [uniqueidentifier] NOT NULL, [StoreID] [uniqueidentifier] NOT NULL, [DateAdded] [datetime] NOT NULL, [AddedByID] [uniqueidentifier] NOT NULL, [LastUpdated] [datetime] NOT NULL, [LastUpdatedByID] [uniqueidentifier] NOT NULL, [Title] [nvarchar](150) NOT NULL, [Term] [nvarchar](150) NOT NULL, [Content] [ntext] NULL ) exec sp_executesql N'INSERT INTO CustomPages (Title, Term, Content, LastUpdated, DateAdded, StoreID, LastUpdatedById, AddedById, ID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)',N'@p0 nvarchar(21),@p1 nvarchar(21),@p2 nvarchar(33),@p3 datetime,@p4 datetime,@p5 uniqueidentifier,@p6 uniqueidentifier,@p7 uniqueidentifier,@p8 uniqueidentifier',@p0=N'Size and Colour Chart',@p1=N'size-and-colour-chart',@p2=N'This is the size and colour chart',@p3=''2009-03-14 14:29:37:000'',@p4=''2009-03-14 14:29:37:000'',@p5='48315F9F-0E00-4654-A2C0-62FB466E529D',@p6='1480221A-605A-4D72-B0E5-E1FE72C5D43C',@p7='1480221A-605A-4D72-B0E5-E1FE72C5D43C',@p8='1E421F9E-9A00-49CF-9180-DCD22FCE7F55' </code></pre> <p>In response the the answers/comments, I am using Fluent NHibernate and the generated mapping is below</p> <pre><code> public CustomPageMap() { WithTable("CustomPages"); Id( x =&gt; x.ID, "ID" ) .WithUnsavedValue(Guid.Empty) . GeneratedBy.Guid(); References(x =&gt; x.Store, "StoreID"); Map(x =&gt; x.DateAdded, "DateAdded"); References(x =&gt; x.AddedBy, "AddedById"); Map(x =&gt; x.LastUpdated, "LastUpdated"); References(x =&gt; x.LastUpdatedBy, "LastUpdatedById"); Map(x =&gt; x.Title, "Title"); Map(x =&gt; x.Term, "Term"); Map(x =&gt; x.Content, "Content"); } &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="MyNamespace.Core" namespace="MyNamespace.Core"&gt; &lt;class name="CustomPage" table="CustomPages" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;id name="ID" column="ID" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000"&gt;&lt;generator class="guid" /&gt;&lt;/id&gt; &lt;property name="Title" column="Title" length="100" type="String"&gt;&lt;column name="Title" /&gt;&lt;/property&gt; &lt;property name="Term" column="Term" length="100" type="String"&gt;&lt;column name="Term" /&gt;&lt;/property&gt; &lt;property name="Content" column="Content" length="100" type="String"&gt;&lt;column name="Content" /&gt;&lt;/property&gt; &lt;property name="LastUpdated" column="LastUpdated" type="DateTime"&gt;&lt;column name="LastUpdated" /&gt;&lt;/property&gt; &lt;property name="DateAdded" column="DateAdded" type="DateTime"&gt;&lt;column name="DateAdded" /&gt;&lt;/property&gt; &lt;many-to-one name="Store" column="StoreID" /&gt;&lt;many-to-one name="LastUpdatedBy" column="LastUpdatedById" /&gt; &lt;many-to-one name="AddedBy" column="AddedById" /&gt;&lt;/class&gt;&lt;/hibernate-mapping&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload