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.

Applying Open Graph or “How to share pretty info?”

After working on a new website yesterday I was ready to share my progress with some friends and I was dismayed that the link produced on Facebook was not pretty, the way other site are automatically linked with a nice picture and the right kind of info. Not to be discouraged I figured I had to do better…

The Open Graph Protocol

A little searching led to the Open Graph protocol: “The Open Graph protocol enables any web page to become a rich object in a social graph. For instance, this is used on Facebook to allow any web page to have the same functionality as any other object on Facebook.”

So in other words: the regular meta tags I was using were not helping I needed to add ogp.me tags.

There is some help on page, but a basic example for your homepage is not on the OGP.ME webpage so I figured I’d share my result:

    <meta property="og:title" content="Chianso: digitaal kindvolgsysteem, documentbeheer en kinddossier." />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="https://www.chianso.nl/" />
    <meta property="og:image" content="https://www.chianso.nl/Images/ChiansoLogoShareX256.jpg" />
    <meta property="og:site_name" content="Chianso" />
    <meta property="og:description" content="Chianso biedt een digitaal kindvolgsysteem, online documentbeheer en digitaal kinddossier waarmee voldaan wordt aan nieuwe IKK regelgeving, kwaliteitsverbetering op de groep en kostenbesparing door het terugbrengen van administratieve handelingen." />
    <meta property="og:locale" content="nl_NL" />


Now what I found is that the image you want to share cannot be larger than 8MB, also, even though I found no documentation for this, JPG images seem to work better than PNG.

Facebook debugger

So one way of getting this right is adding the info to your website and the trying to share a link on Facebook. This will work once… then the next try may not show any of your changes. A very useful tool at this point is the Facebook Debugger. It will load all the metadata from your site and provide a link preview. Together with a bunch of hints and useful info AND it offers a ‘Scrape Again’ button, which invalidates the caching that is in play.

You can find the debugger here:  https://developers.facebook.com/tools/debug/sharing




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!

Three issues (with solutions) when upgrading to Visual Studio 2017

In the last 2 weeks I’ve upgraded two (large) C# / ASP.NET solutions from Visual Studio 2015 to Visual Studio 2017. Good news? By and large things ‘just’ work. But there were three things that required attention:

  1. Issue: My project used PowerShell projects from “PowerShell Tools for Visual Studio 2015“. This VS2015 extension did not automatically get activated for VS2017.

    1. Download and reinstall the extension. The installer recognized that VS2017 was now installed and enabled the extension for VS2017.
  2. Issue: The projects were both using the Microsoft Unit Test framework and the unit test runner would run without issue in VS2017, but Live Unit Testing would not work.

    1. In the Unit Test project(s) remove the reference to: Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
    2. Use nuget to add MSTest.TestAdapter and MS.TestFramework to the unit test project:
  3. Issue: My Team Services CI build failed after I started using C#7 syntax in my code.
    Solution: Install Visual Studio 2017 on the build server and change the build definition to use Visual Studio 2017 for building the solutions.
    Alas, this will not (yet) work if you’re using a Hosted Agent. So be sure to be using a build agent that runs on a machine where Visual Studio 2017 is installed.

Useful to know:

  • For a short time some team members were working with VS2015 and some with VS2017. No problem as long as you’re not using C#7 syntax just yet 🙂
  • Play around with including/excluding tests from Live Unit Testing, for larger solutions the performance hit of having all the tests being run all the time seems prohibitive.

Get current, future or most recent item

In the application I’m working on I had to repeatedly show information based on relevance by date. I wrote a little extension method to help select the current item, if there is no current item, then select the first future item, if there are no future items, then select the most recent item in the past.

public static class RelevantItemExtension
    public static T GetCurrentFutureOrMostRecent( this IEnumerable list,
                                                  Func<T, DateTime> dateSelector )
                                                  where T : class
        if ( list.Count() == 0 )
            return null;
        else if ( list.Count() == 1 )
            return list.First();
            var currentOrFuture =
                list.Where( x => dateSelector( x ).Date >= DateTime.Today.Date )
                    .OrderBy( x => dateSelector( x ) )

            if ( currentOrFuture != null )
                return currentOrFuture;
                return list.OrderBy( x => dateSelector( x ) )

I’m able to use this method as follows:

public void RelevantItemExtension_Future()
    // Arrange
    List list = new List() {
        new DataItem() { Value = 1, Date = DateTime.Today.AddDays(-21) },
        new DataItem() { Value = 2, Date = DateTime.Today.AddDays(-7) },
        new DataItem() { Value = 3, Date = DateTime.Today.AddDays(-14) },
        //new DataItem() { Value = 4, Date = DateTime.Today },
        new DataItem() { Value = 5, Date = DateTime.Today.AddDays(7) }

    // Act
    var item = list.GetCurrentFutureOrMostRecent( x => x.Date );

    // Assert
    Assert.AreEqual( 5, item.Value );

HoloLens – Hello World

What a day it has been… today I unboxed my HoloLens and started my augmented reality adventures.

This slideshow requires JavaScript.

After going through the tutorial and learning about Gaze, Gestures and Voice commands and then ofcourse some initial playing around with Holograms, RoboRaid and my first HoloLens Skype call it was time to get ready and start work on my first HoloLens app.

But first I needed to get the right tools:

  • Install the tools on the Windows Dev Center is good place to start, it offers links to the most recent versions of the HoloLens Emulator and Unity HoloLens (currently still in technical preview).
  • Microsoft HoloLens application for Windows is an awesome tool the let’s you live stream the video from your HoloLens to your PC and makes it easy to create videos of your augmented world.

After updating Visual Studio, installing the emulator, installing Unity 3D and creating a Unity account and for good measure, following Unity 3D on Twitter, I figured I was ready to go.

I watched the welcome to Unity video and figured as a coder it might be easier to start with a simple Visual Studio only trial app. Well…. After looking at the DirectX 11 template app  that comes with installed with the SDK and running a colorful cube in the HoloLens emulator I quickly figured I needed an easier start so I build a 2D Universal Windows Application with my “Hello AR World” message. On this page I found how to an over the air WiFi deploy directly from Visual Studio to the HoloLens, nice!

Oh, and after messing around with ‘transparent’ settings for the background of my Window I suddenly remember something Dennis had told me: HoloLens knows no black and so black is considered transparent.


Note: The screen capture shows the transparent window much darker than how you really see it when you wear the HoloLens.

Tip: use Cortana if you need to reboot, just say ‘Hey Cortana’ followed by ‘Reboot’.

Also, I have a feeling my laptop is going to need an upgrade soon… 8GB of memory appears to be barely enough to run the necessary HoloLens emulator, twice I’ve had to restart my machine and make sure that the emulator is started first, before starting other apps.

Next step… building a Unity 3D application… to be continued 🙂


Unlocking VSTS files from a user that left the team

In the past I had used the tf.exe command line tool to unlock files that were locked by a co-worker that had left.

Today I discovered a cool free tool that makes it much easier to delete obsolete workspaces. By deleting the workspace all locks held by the workspace are automatically released.

Go to http://www.attrice.info/cm/tfs/index.htm to download the Attrice Team Foundation Sidekicks.

The Sidekicks tool worked excellent against our online Visual Studio Team Services repository.