Page 1 of 1

EF Code First Oracle Map Char(1) to Bool

Posted: Thu 23 May 2013 21:47
by degas
I am using EF Code First with Oracle and i want to map Char(1) to bool. The values are 'S' for true and 'N' for false

Re: EF Code First Oracle Map Char(1) to Bool

Posted: Mon 27 May 2013 15:23
by Shalex
There is a solution for mapping System.Boolean to CHAR(1) with values 'Y' for true and 'N' for false. We have added a new "yes no char as boolean" datatype which can be used in both Database-First and Code-First approaches.
degas wrote:I am using EF Code First with Oracle and i want to map Char(1) to bool.

Code: Select all

  public class BoolTable {
 
    public int Id { get; set; }
 
    [Column(TypeName = "yes no char as boolean")]
    public bool CharBasedBoolean { get; set; }
 
  }
 
  public class MyContext: DbContext {
 
    public DbSet<BoolTable> BoolTable { get; set; }
 
    // ...
  }
The following DDL will be generated basing on the BoolTable class:

Code: Select all

CREATE TABLE "BoolTables" ( 
  "Id" NUMBER(10) NOT NULL,
  "CharBasedBoolean" CHAR(1) NOT NULL,
  PRIMARY KEY ("Id")
)
Entity Framework 6 introduced the possibility of using custom conventions for setting mapping. This is convenient if all System.Boolean properties in different model classes are mapped to CHAR(1) column. In this case you can employ lightweight convention (which is applied for all System.Boolean properties) instead of setting ColumnAttribute for each property separately:

Code: Select all

  public class MyContext : DbContext {
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
 
      modelBuilder
        .Properties()
        .Where(p => p.PropertyType == typeof(Boolean))
        .Configure(p => p.HasColumnType("yes no char as boolean"));
    }
 
    // ...
  }

Re: EF Code First Oracle Map Char(1) to Bool

Posted: Wed 06 Aug 2014 13:29
by dumian
Hi ,

How was the Problem Solved to use 'S' instead of 'Y' , because we have an simillar case to use 'J' instead of 'Y' ?

regards,
Johannes

Re: EF Code First Oracle Map Char(1) to Bool

Posted: Thu 07 Aug 2014 14:59
by Shalex
dumian wrote:How was the Problem Solved to use 'S' instead of 'Y' , because we have an simillar case to use 'J' instead of 'Y' ?
Our solution works only for mapping System.Boolean to CHAR(1) with values 'Y' for true and 'N' for false.
As a workaround, we recommend you to update CHAR(1) columns in your tables to replace 'S' with 'Y' and 'J' with 'Y'.