20

I have a column like this in 1 of my database tables

DateCreated, datetime, default(GetDate()), not null

I am trying to use the Entity Framework to do an insert on this table like this...

        PlaygroundEntities context = new PlaygroundEntities();

        Person p = new Person
        {
            Status = PersonStatus.Alive,
            BirthDate = new DateTime(1982,3,18),
            Name = "Joe Smith"
        };

        context.AddToPeople(p);
        context.SaveChanges();

When i run this code i get the following error

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.

So i tried setting the StoreGeneratedPattern to computed... same thing, then identity... same thing. Any ideas?

5 Answers 5

9

You have to manually edit the edmx xml and set your SSDL StoreGeneratedPattern attributes to identity or computed. But whenever you update your edmx via the designer your changes will get overwritten.

This is a known issue. Please see the following links for more details:

Microsoft Connect Ticket

Using a GUID as an EntityKey in Entity Framework 4

2
  • Visual Studio 2010 lets you set this now without having to edit the edmx file. Mar 3, 2011 at 15:55
  • @ICodeForCoffee as KenB mentions, there is currently a bug and this setting in the Visual Studio UI is not correctly applied to the .edmx file. Really annoying :[ May 11, 2011 at 22:39
5

I had the same problem! For me it works like this:

Database MS SQL Server express:

[RowTime]  [datetime2](7)  NOT NULL

ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [DF_Table_1_RowTime]  DEFAULT (getdate()) FOR [RowTime]
GO

Then I import the table from database to my Entities model. Entities will not realise the default value! So, you have to set the StoreGeneratedPattern of the column to Computed. Then Entities will not put there any default value any more.

Combination of:

datetime2,
NOT NULL, 
StoreGeneratedPattern=Computed

Works for me!

2

Changing type of DateCreated to datetime2 might solve the problem.

datetime 2007-05-08 12:35:29.123

datetime2 2007-05-08 12:35:29. 12345

Ref: http://technet.microsoft.com/en-us/library/bb677335.aspx67

5
  • that solves the exception, but the default (GetDate()) is not getting called on the insert. Apr 29, 2010 at 20:46
  • actually, it appears it does work, but the .net code is setting like the datetime.minvalue by default. Is there a way to prevent it from doing this? Apr 29, 2010 at 21:02
  • 2
    I have this exact problem, can you write up a proper answer please wcpro?
    – Loofer
    May 26, 2010 at 10:33
  • 1
    @wcpro How exactly did you configure the storegeneratedpattern to work around this problem?
    – chakrit
    Jan 26, 2011 at 22:23
  • in the edmx file, right-clicking on the field -> properties -> change StoreGeneratedPattern from none to Computed and then save... works for me. Jun 16, 2014 at 19:01
2

Here is a working workaround:

1) Change the column to datetime2 as mentioned elsewhere. This fixes the conversion error.

2) Add a trigger that sets DateCreated to getdate();

CREATE TRIGGER [TR_AS_ChangeTime] ON [AS_ApplicationSession] 
  AFTER INSERT,UPDATE AS
BEGIN

  SET NOCOUNT ON;
  UPDATE AS_ApplicationSession 
  SET AS_ChangeTime = getdate() 
  WHERE AS_Id IN(SELECT AS_ID FROM INSERTED)

END

3) If neccessary, set

p.DateCreated = DateTime.MinValue;

just to initialize it.

4) If you need the DateCreated from the database, add

context.Refresh(System.Data.Objects.RefreshMode.StoreWins, p);

just after

context.SaveChanges();
1

Focusing on the fact that I do not want change the database, since it is an application problem and I expect them to solve this problem one day, my solution (that is totally possible in my case) is to create a partial class of the model to correct the problem on constructor:

public partial class Log
{
    public Log()
    {
        this.Date = DateTime.Now;
    }
}

This works for me because:

  • I create the model on the moment I send it to database.
  • I use CLOUD for these services, the datetime must be the same in both Application and Database servers!

Don't forget that the namespace needs to match the Model namespace or partial should not list properties (and should no be partial as well ;])!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.