0

I have a method that gets an existing row from a table in my database and updates some values on it and then save those changes. The table in quesiton has these columns:

columns in table

The code that does the updating is here:

        public void Update(Accommodation accommodation, string code, int supplierId)
        {
            var existingAccommodation = Get(a => a.Code == code && a.SupplierId == supplierId);

            DateTime now = DateTime.Now;

            existingAccommodation.ModifiedDate = now;
            existingAccommodation.Description = accommodation.Description;
            existingAccommodation.Introduction = accommodation.Introduction;
            existingAccommodation.Name = accommodation.Name;
            existingAccommodation.Strapline = accommodation.Strapline;
            existingAccommodation.Type = accommodation.Type;
            existingAccommodation.Processed = true;

            DataContext.SaveChanges();
        }

The problem is that the line DataContext.SaveChanges(); causes an exception whose innerexception says:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

This is where the above code is called

            Accommodation existingAccommodation = GetByCode(code, supplierId);
            if (existingAccommodation != null)
            {
                _accommodationRepository.Update(
                    accommodation, code, existingAccommodation.SupplierId);
            }
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • Can you post the code where you call the `Update` method and the argument `accommodation` gets created. This error usually means that you have DateTime.Min value in of the entity fields. Maybe you save multiple entities in your method... – nemesv Aug 23 '12 at 16:51
  • done - please take a look. Is there anything I'm missing? – Sachin Kainth Aug 23 '12 at 16:59
  • It is only 1 entity I save and the two dates modfieddate and createddate are populated. – Sachin Kainth Aug 23 '12 at 16:59
  • I see no `CreationDate` being set, though. Is it sql-server 2005 maybe? – Gert Arnold Aug 23 '12 at 18:04
  • I figured out the problem. The problem was that I had another Accommodation object that I "newed" just before calling the Update method and it was that object that EF was trying to save as well. – Sachin Kainth Aug 29 '12 at 09:31

0 Answers0