Elvin Mammadov
Elvin Mammadov

Reputation: 27387

Fluent NHibernate: Mapping Bool Property to ORACLE DB

I want to mapping bool c# property to Oracle DB. I tried several methods for this. But didn't work. There is no example on web for best solution. My entity as bellow:

 public class Department : EntityBase<short>
{
    [JsonIncludeProperty(NameOfSet = "list")]
    public virtual string Name { get; set; }

    public virtual  Department ParentDepartment { get; set; }
    public virtual IList<Department> ChildDepartments { get; set; }
    // Bool value
    public virtual bool IsActive { get; set; }
}

From this Fluent mapping example:

public class DepartmentMap : ClassMap<Department>
{
    public DepartmentMap()
    {
        Table("DEPARTMENTS");
        LazyLoad();
        Id(x => x.Id)
            .GeneratedBy.Sequence("DEPARTMENTS_SEQ")
            .Not.Nullable()
            .Column("DEPARTMENT_ID")
            .CustomSqlType("NUMBER(5,0)");
        Map(x => x.Name).Not.Nullable().Column("DEPARTMENT_NAME").CustomSqlType("NVARCHAR2(100)");

        Map(department => department.IsActive).Not.Nullable().Column("IS_ACTIVE").CustomType<YesNoType>();

        HasMany(x => x.ChildDepartments)
            .AsList()
            .Inverse()
            .Cascade.All()
            .KeyColumn("PARENT_DEPARTMENT_ID");

        References(x => x.ParentDepartment, "PARENT_DEPARTMENT_ID");
    }
}

But this and other methods do not work. Please help me.

Upvotes: 3

Views: 1339

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

The example uses a user type. It needs to be implemented somewhere.

By default, booleans are stored in number columns. From Oracle8iDialect.cs (this line is derived by 9i and 10i):

RegisterColumnType(DbType.Boolean, "NUMBER(1,0)");

By default, you need such a number column in the database, then you can map it without special type configuration:

  Map(department => department.IsActive).Not.Nullable().Column("IS_ACTIVE");

If you want to have another type in the database, you can specify an SQL type or your own user type.

The simplest way to have your mappings and database schema aligned is by generating the schema from the mappings using the NHibernate schema generator. You can only create an SQL create tables script for review purposes if you don't like your schema being created fully automatically.

Upvotes: 2

Related Questions