What is easiest way to deal with converting 0/1 to False/True in EF 4.x?

Viewed 10294

The stored Proc returns a column with the value to be either 0 or 1 without converting to BIT. In my POCO, if I declare the field as

public bool MyColumn {get; set;}

I am getting this error:

The specified cast from a materialized 'System.Int32' type to the 'System.Boolean' type is not valid.

This actually makes sense since EF recognizes the returned value as an integer.

I am wondering that is there any easy way to (add annotation or use fluent api maybe) automatically convert 0/1 to False/True in the mapping behind the scene without touching the Proc?

Thanks in advance!

5 Answers

In your ApplicationDbContext (the class that inherits from DbContext) you can use Fluent Api to convert values for the database.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  modelBuilder.Entity<TheNameOfYourModelClass>()
    .Property(p => p.MyColumn)
    .HasConversion(
       v => v ? 1 : 0,
       v => (v == 1));
}

Now, your database will contain 1 when inserting a true for MyColumn and vice-versa. When reading from your database 1 will be converted to true and vice-versa.

building on what @jimmy provided, could also define the ValueConverter separately and then apply across multiple entities/properties:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var boolConverter = new ValueConverter<bool, int>(
        v => v ? 1 : 0,
        v => (v == 1) ? true : false);

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        foreach (var property in entityType.GetProperties())
        {
            if (property.ClrType == typeof(bool))
                property.SetValueConverter(boolConverter);
        }
    }
}
Related