Entity Framework Core / Value Conversion "InvalidCastException" boolean to int

c# db2 entity-framework-core

Question

i have a little bit of trouble with my EF Core about the value conversion from bool to int.

The background is that I'm using IBM's DB2 as my database and the older versions don't support the BIT type, so I have to use something else to simulate a boolean. In my case I just wan't to use 1 and 0.

I want to do this with the EF Core Value Conversion. My Converter class:

var myconverter = new ValueConverter<bool, int>(x => x ? 1 : 0, x => x == 1);
builder.Entity<ApplicationUser>(b => {
      b.Property(p => p.EmailConfirmed).HasConversion(myconverter);
});

So, the boolean is my model type (TModel) and the int the database type (TProvider).

After executing this simple line of code:

var user = identityContext.Users.First();

I get the error:

InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Boolean'.

And I have no idea why. An insert works fine and the proptery is stored with the right value (1/0) but the select part breaks.

The use of the inbuilt class BoolToZeroOneConverter<int> results in the same error.

EF Core is up to 2.1.1

Am I missing something? Thank's for your help.

UPDATE ApplicationUser inherits "IdentityUser"

public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{

    public IdentityUser();

    public IdentityUser(string userName);

    public virtual DateTimeOffset? LockoutEnd { get; set; }

    [PersonalData]
    public virtual bool TwoFactorEnabled { get; set; }

    [PersonalData]
    public virtual bool PhoneNumberConfirmed { get; set; }

    [ProtectedPersonalData]
    public virtual string PhoneNumber { get; set; }

    public virtual string ConcurrencyStamp { get; set; }

    public virtual string SecurityStamp { get; set; }

    public virtual string PasswordHash { get; set; }

    [PersonalData]
    public virtual bool EmailConfirmed { get; set; }

    public virtual string NormalizedEmail { get; set; }

    [ProtectedPersonalData]
    public virtual string Email { get; set; }

    public virtual string NormalizedUserName { get; set; }

    [ProtectedPersonalData]
    public virtual string UserName { get; set; }

    [PersonalData]
    public virtual TKey Id { get; set; }

    public virtual bool LockoutEnabled { get; set; }

    public virtual int AccessFailedCount { get; set; }

    public override string ToString();
}
1
1
9/6/2018 10:26:28 PM

Popular Answer

We had the same issue and the only thing that worked was to disable the value conversion and to create our own properties with the same name but the expected type, the compiler throws a warning but it was the only way we made it work.

public class ApplicationUser : IdentityUser {

    //Hack: Used as a workaround with DB2's data types
    public int LockoutEnabled { get; set; }
    public int EmailConfirmed { get; set; }

}
0
10/25/2018 12:23:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow