What data type is used to store long strings in Oracle?

Viewed 2441

I am developing using .net core. The database uses Oracle. The maximum value of varchar2 is 4000 bytes, and what data type is used to store the data above this?

I created a column using clob and mapped it to .net.

Model

public string CONTENTS { get; set; }

code

try
{
    var content= _mapper.Map<T_CONTENT>(post);

    _dbcontext.T_CONTENT.Add(content);
    _dbcontext.SaveChanges();
    transaction.Commit();

    return content.ID;
}
catch (Exception e)
{
    transaction.Rollback();
    return null;
}

public class contentDbContext : DbContext
{
    public contentDbContext(DbContextOptions options)    : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }

    public DbSet<T_CONTENT> T_CONTENT{ get; set; }
}

When I save the data, I get the error below. error message : ORA-01460: unimplemented or unreasonable conversion requested

3 Answers

In the mapping you can try using .HasColumnType("CLOB") with your modelBinder

Example:

public class ContentDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<<EntityName>>()
            .Property(a => a.<CoulmnName>).HasColumnType("CLOB");
    }
}

According to the ODP.NET EF Core Migrations Data Type Mapping, the default string type mapping without data annotations / fluent configuration is NVARCHAR2(2000).

Depending of the values of IsUnicode, IsFixedLength and HasMaxLength, it could be CHAR[n], NCHAR[n], VARCHAR2[n], NVARCHAR2[n], CLOB and NCLOB.

So if you need long strings with MaxLength >= 2000 characters, you need to either specify the max length >= 2000 with data annotations ([MaxLength] or [StringLength], not sure which one, you have to figure out) or HasMaxLength fluent API - either should give you CLOB or NCLOB storage data type. Or, specify directly the storage data type via TypeName argument of the [Column] attribute, or HasColumnType fluent API.

From the two methods preferable is to do that via specifying max length, since it is more abstract and database agnostic, and better represents the desired behavior without dealing with database specific types.

Related