简体   繁体   English

实体框架核心/值转换“ InvalidCastException”布尔值转换为整数

[英]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. 我的EF Core在将值从bool转换为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. 背景是我使用IBM的DB2作为数据库,而较早的版本不支持BIT类型,因此我必须使用其他方法来模拟布尔值。 In my case I just wan't to use 1 and 0. 就我而言,我只是不想使用1和0。

I want to do this with the EF Core Value Conversion. 我想通过EF核心价值转换来做到这一点。 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). 因此,布尔值是我的模型类型(TModel),而int是数据库类型(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'. InvalidCastException:无法将类型为System.Int32的对象转换为类型为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. 插入件可以正常工作,并且以正确的值(1/0)存储属性,但是选择部分破裂。

The use of the inbuilt class BoolToZeroOneConverter<int> results in the same error. 内置类BoolToZeroOneConverter<int>导致相同的错误。

EF Core is up to 2.1.1 EF Core最高为2.1.1

Am I missing something? 我想念什么吗? Thank's for your help. 谢谢你的帮助。

UPDATE ApplicationUser inherits "IdentityUser" UPDATE ApplicationUser继承“ 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; }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架核心 DbContext.SaveChanges 抛出 System.InvalidCastException:无法将 System.Boolean 类型的对象转换为 System.Int16 - Entity Framework Core DbContext.SaveChanges throws System.InvalidCastException: Unable to cast object of type System.Boolean to type System.Int16 将 nvarchar 值“EnumValue”转换为数据类型 int 时,Entity Framework Core 3.1 枚举转换失败 - Entity Framework Core 3.1 Enum Conversion failed when converting the nvarchar value 'EnumValue' to data type int Entity Framework Core linq 查询返回 InvalidCastException - Entity Framework Core linq query returns InvalidCastException 如何使用Entity Framework Core在具有defaut值的布尔值上设置另一个值? - How to set another value on a boolean with defaut value with Entity Framework Core? 实体框架返回 InvalidCastException - Entity Framework returns InvalidCastException Entity Framework Core 3.1 从存储过程返回值 (int) - Entity Framework Core 3.1 Return value (int) from stored procedure 实体框架:boolean 值为真 - Entity Framework : boolean value is true 转换错误为int? 在实体框架中字符串 - Conversion error int? to string in Entity Framework 使用实体框架时的InvalidCastException - InvalidCastException when using Entity Framework 实体框架 System.InvalidCastException - Entity Framework System.InvalidCastException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM