简体   繁体   中英

Entity Framework Core / Value Conversion “InvalidCastException” boolean to int

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();
}

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; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM