EF Code First Oracle Map Char(1) to Bool

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
degas
Posts: 77
Joined: Mon 16 Feb 2009 18:36
Location: Argentina

EF Code First Oracle Map Char(1) to Bool

Post by degas » Thu 23 May 2013 21:47

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

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Mon 27 May 2013 15:23

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"));
    }
 
    // ...
  }

dumian
Posts: 13
Joined: Sat 09 Jun 2012 19:10
Contact:

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

Post by dumian » Wed 06 Aug 2014 13:29

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

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Thu 07 Aug 2014 14:59

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'.

Post Reply