Search code examples
asp.net-coreasp.net-core-mvcsqlclientasp.net-core-3.1

Asp.Net Core 3.1 MVC web application compile error with SqlDateTime


I am having trouble migrating my web app from 2.2 to 3.1 regarding the use of SqlDateTime. To make sure it wasn't an issue with my migration I created a new MVC web application using the standard asp.net core 3.1 template in Visual Studio 2019 16.4. When I add a variable and assign it to SqlDateTime I get a compilation error...

The type 'SqlDateTime' exists in both 'System.Data.Common, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Previously it used to work when using System.Data.SqlClient but I know that this has been replaced in 3.0 with Microsoft.Data.SqlClient however the latter doesn't have SqlDateTime so the only way to get it is by using System.Data.SqlTypes but this produces the compilation error. I haven't even included System.Data.SqlClient so I'm a bit confused as to why it is being referenced by the compiler anyway. Any ideas?

enter image description here


Solution

  • You have 2 options.

    1. Downgrade to 3.0, it's not supported in 3.1.

    2. Make your own struct with the MinValue of January 1, 1753

    Check here for the value and supported versions