EF Code First Oracle Map Char(1) to Bool
EF Code First Oracle Map Char(1) to Bool
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
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.
The following DDL will be generated basing on the BoolTable class:
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:
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; }
// ...
}
Code: Select all
CREATE TABLE "BoolTables" (
"Id" NUMBER(10) NOT NULL,
"CharBasedBoolean" CHAR(1) NOT NULL,
PRIMARY KEY ("Id")
)
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
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
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
Our solution works only for mapping System.Boolean to CHAR(1) with values 'Y' for true and 'N' for false.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' ?
As a workaround, we recommend you to update CHAR(1) columns in your tables to replace 'S' with 'Y' and 'J' with 'Y'.