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

In my application I import files, process them and save them to the database. This leads to large amounts of inserts and updates on the database. It can be very frustrating to have Entity Framework reject all update with a message: “The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. “.
This message usually means a DateTime.MinValue is being inserted into a SQL DateTime column. The problem is to figure out which entities are causing the problem.

To help with this I wrote some code to inspect the pending changes. Luckily me and my team have been consistent when it comes to using “Date” in a property name wherever applicable.


// first get all the changes (added or modified)
var changes = MyEntities.ChangeTracker.Entries().Where( x => ( x.State == System.Data.Entity.EntityState.Modified ||
																x.State == System.Data.Entity.EntityState.Added )).ToList();

// look for all changes involving date field
var entitiesWithDate = ( from c in changes
							where c.CurrentValues.PropertyNames.Where( pn => pn.Contains( "Date" ) ).Any() == true //
							select c ).ToList();

// select all entities where datetime value is before 1/1/1000 which will be rejected by SQL Server
// I use 1/1/1000 because DateTime.MinValue did not seem to work
var entitiesWithMinValueDate = ( from e in entitiesWithDate
									from p in e.CurrentValues.PropertyNames.Where( pn => pn.Contains( "Date" ) )
									where e.CurrentValues[p] != null && (DateTime) e.CurrentValues[p] < new DateTime( 1000, 1, 1 )
									select e ).ToList();



Notice I put #if DEBUG around the code, we don’t need this unless we’re debugging.

Reinstall NuGet packages after targeting newer .NET Framework

I recently upgraded all projects in a solution to .NET 4.7 and started seeing some NuGet warnings:

target net

Essentially what seems to happen is that the NuGet package was installed when .NET 4.6 was the runtime target and this package may or may not need to be reinstalled (everything was functioning, but I just don’t like warnings when compiling).

Turns out there is no UI button for reinstalling a package, so the Package Manager Console is the place to go. The following command will reinstall the appropriate package for all project that use the package:

Update-Package -Id <package_name> –reinstall

Note that as soon as you’ve typed -Id you can use <Tab> to get a drop down list of installed packages.

For more variations of the command see: https://docs.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages

Tracking active document in Visual Studio 2017

I used to have active work item tracking turned on in my Solution Explorer, but that can get annoying, so I turned it off, but then I found I spend too much time searching for my active document. I figured there must be an easier way. Sure enough, Visual Studio has got me covered on this…

So, active item tracking is set in the configuration settings:


As you see, I have turned it off.

Now when you have an active document open in the editor, the Solution Explorer will start a bi-directional arrow icon:


Press it to have the solution explorer track down the active document in the solution explorer. The feature is called ‘Sync with Active Document’. Also accesible via keyboard shortcut: CTRL+[, S



Get the day of the week, in the right language

I’m working on an app that at some point will need to work in multiple languages and needed to display the day of the week to the user. I started with:

string label = DateTime.Today.DayOfWeek.ToString()

But that give me the name of the enumeration, which is of course in English. In order to get the name based on the current language setting I needed the following code:

var culture = System.Globalization.CultureInfo.CurrentCulture;
string label = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);

Happy coding!