3

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.

1
  • 1
    "didn't work"/"not work" is way too short for an explanation of what goes wrong. See this page, referenced by SO how to ask. Commented Mar 10, 2016 at 17:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.