0

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!

Brandlarm
  • 9
  • 3
  • Seems that Xamarin reads mssql datetime2 column values as string. Still haven't found a proper solution, but if someone needs a workaround you can annotate your EF dbset datetime properties with [Column(TypeName = "datetime")]. This will make EF create a datetime instead of datetime2 column on the database, which Xamarin doesn't have a problem with. – Brandlarm Sep 10 '17 at 18:53
  • Your Android application is directly linked to your SQL Server? – hugo Sep 11 '17 at 07:07
  • This is just me trying out things at home, it's not a "real" product. In that case I would probably have used a WebAPI for the Xamarin project to call. To answer you question, I have a .NET Standard 2 class library as a reference in my Xamarin project, this contains EF and and the db connection. So I guess it's somewhat directly linked yeah. This is my first time trying Xamarin/Android, so I'm not really sure how it all works yet, but it runs fine now (security issues aside?). :) – Brandlarm Sep 11 '17 at 08:15

0 Answers0