InvalidCastException on DateTime using Entity Framework/Xamarin Android (reads date as string)

315 views Asked by At

trying out Xamarin for the first time and can't really figure this one out.

I am working on a solution that contains 3 projects. One .NET standard 2 class library with Entity Framework, that I use as a service layer, which the other projects reference and calls to get its data objects.

The other two projects are a .NET Core 2 Web project and a Xamarin.Android project.

The .NET Core 2 project can retrieve all data without any issues, while the Xamarin.Android project get InvalidCastException when an object contains a DateTime property.

System.InvalidOperationException: An exception occurred while reading a database value for property 'myObject.LastLogin'. The expected type was 'System.DateTime' but the actual value was of type 'System.String'

I am using the same methods and classes for both the Xamarin.Android project and the .NET Core 2 project. I also tried returning the object from the service layer with the DateTime property set to DateTime.Now always, to ensure it's a proper date.

Same object works fine on Xamarin.Android if I remove the DateTime property. Example of the object returned below.

public class User
    {
        public int ID { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime LastLogin { get; set; }
        public byte[] ProfilePicture { get; set; }
    }

If anyone have any ideas to how I can fix this I would really appreciate it. I am working around it currently, but would really like to have a proper solution to this issue.

Thanks!

0

There are 0 answers