Carpool Project: Part #13
October 13, 2010 Leave a comment
I am closer to the end of the development cycle than the beginning and I have run into a new problem. I created a method that Inserts new practices in the PracticeFactory. When I test it against the server, I get the following error
1 Test method Com.Tff.Carpool.Domain.Tests.PracticeFactoryTest.InsertPracticeTest threw exception: 2 System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 3 The statement has been terminated. 4
I searched MSDN/StackOverflow and I came up with the EF needing to be 2005, not 2008 (here) I switched it and got the same problem. This solution doesn’t make sense to me – b/c the data server is 2008. I then decided to look at the SQL that EF is generating to see if I can find an error in that code. There is a method called ToTraceString() that I can use to inspect the SQL generated – the trick is to locate what object the method belongs to – since it is not part of ObjectContext. The MSDN example here is not much help – I wonder if EF does not expose the method? In addition, I can’t set up tracing on the remote server – it is hosted on WinHost. I then found this post which solved the problem – it is just a feature gap in EF.
Once I had this code up and running, I looked at the results:
1 insert [dbo].[Carpool_Practice]([PracticeDate], [PracticeStartTime], [PracticeEndTime], [DrylandIndicator]) 2 values (@0, @1, @2, @3) 3 select [PracticeId] 4 from [dbo].[Carpool_Practice] 5 where @@ROWCOUNT > 0 and [PracticeId] = scope_identity() 6 @0 = 9/13/2010 7:17:53 AM 7 @1 = 1/1/0001 12:00:00 AM 8 @2 = 1/1/2010 7:00:00 PM 9 @3 = True 10
And here is the bug:
1 private static void MapPractice_Practice(Carpool_Practice practiceToInsert, Practice practice) 2 { 3 practiceToInsert.DrylandIndicator = practice.DryLandIndicator; 4 practiceToInsert.PracticeDate = practice.Date; 5 practiceToInsert.PracticeEndTime = practice.EndTime; 6 practiceToInsert.PracticeStartTime = practiceToInsert.PracticeStartTime; 7 } 8
I changed it to this:
1 practiceToInsert.PracticeStartTime = practice.StartTime;
And it ran. I was assigning the EF value to itself.
<Sound of me slapping my forehead>