OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Connector/NET

V1.1.0

  • What Is OceanBase Connector/NET
    • Introduction
  • Install and Connect
    • Installation process
    • Test the connection
    • Package reference and usage
    • Compatibility impact
  • User Guide
    • Quick Start
      • Complete example
    • Connection Strings and Data Source
      • Connection strings
    • Data Types
      • Overview of data types
    • Command and Parameter
      • Overview
    • Result Set
      • Supported result set types
    • ORM and Frameworks
      • Use Entity Framework Core
      • Use SqlSugar
      • Use FreeSql
  • Reference Information
    • Common Interfaces
      • Overview of commonly used interfaces
  • Version Release Records
    • OceanBase Connector/NET V1.0.0
    • OceanBase Connector/NET V1.1.0

Download PDF

Introduction Installation process Test the connection Package reference and usage Compatibility impact Complete example Connection strings Overview of data types Overview Supported result set types Use Entity Framework Core Use SqlSugar Use FreeSql Overview of commonly used interfaces OceanBase Connector/NET V1.0.0 OceanBase Connector/NET V1.1.0
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Connector/NET
  3. V1.1.0
iconOceanBase Connector/NET
V 1.1.0
  • V 1.1.0
  • V 1.0.0

Use Entity Framework Core

Last Updated:2026-04-28 03:38:03  Updated
share
What is on this page
Install
Connection string
Get started
Example
Execute DML statements
Migrations and database creation
Value generation
Code First / Data type mapping
Considerations
1. Default string mapping
2. decimal precision and scale
3. NOT NULL string columns
4. ExecuteSqlRaw and SELECT
Summary

folded

share

OceanBase.EntityFrameworkCore8 is an EF Core provider for OceanBase Oracle mode, based on OceanBase Connector/NET. It allows you to use OceanBase.OracleConnection, OracleCommand, and other capabilities in EF Core to access OceanBase.

Note

Currently, this provider only supports EF Core 8.0.

Install

If you install it by using NuGet:

dotnet add package OceanBase.EntityFrameworkCore8

Connection string

Use the Oracle mode format supported by OceanBase.ManagedDataAccess. Replace the placeholders with the actual values in your environment. Do not directly copy the literals in the sample:

server=<host or IP address>;port=<port number>;user id=<OceanBase Oracle account>;password=<password>;database=<schema name>;

Note

  • user id specifies the OceanBase Oracle account.
  • database is recommended. The metadata capabilities of the current provider rely on it to identify the current schema.
  • The default port is 2881.

Get started

Define entities and a DbContext:

using Microsoft.EntityFrameworkCore;
using OceanBase.EntityFrameworkCore;

public sealed class User
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Balance { get; set; }
    public DateTime CreatedTime { get; set; }
}

public sealed class AppDbContext : DbContext
{
    private readonly string _connectionString;

    public AppDbContext(string connectionString)
    {
        _connectionString = connectionString;
    }

    public DbSet<User> Users => Set<User>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseOceanBaseForOracle(_connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>(entity =>
        {
            entity.ToTable("USERS");
            entity.HasKey(x => x.Id);

            entity.Property(x => x.Id)
                .HasColumnName("ID");

            entity.Property(x => x.Name)
                .HasColumnName("NAME")
                .HasMaxLength(100)
                .IsRequired();

            entity.Property(x => x.Balance)
                .HasColumnName("BALANCE")
                .HasColumnType("NUMBER(10,2)");

            entity.Property(x => x.CreatedTime)
                .HasColumnName("CREATED_TIME")
                .HasDefaultValueSql("SYSDATE");
        });
    }
}

Use dependency injection:

using Microsoft.EntityFrameworkCore;
using OceanBase.EntityFrameworkCore;

builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseOceanBaseForOracle(connectionString));

Example

Query, insert, update, and delete data:

await using var db = new AppDbContext(connectionString);

var adults = await db.Users
    .Where(x => x.Balance > 100)
    .OrderBy(x => x.Name)
    .ToListAsync();

var user = new User
{
    Name = "Alice",
    Balance = 7500.50m,
    CreatedTime = DateTime.Now
};

db.Users.Add(user);
await db.SaveChangesAsync();

user.Name = "Alice Zhang";
await db.SaveChangesAsync();

db.Users.Remove(user);
await db.SaveChangesAsync();

Perform transactions:

await using var db = new AppDbContext(connectionString);
await using var tx = await db.Database.BeginTransactionAsync();

try
{
    db.Users.Add(new User { Name = "U1", Balance = 100m, CreatedTime = DateTime.Now });
    db.Users.Add(new User { Name = "U2", Balance = 200m, CreatedTime = DateTime.Now });

    await db.SaveChangesAsync();
    await tx.CommitAsync();
}
catch
{
    await tx.RollbackAsync();
    throw;
}

Execute DML statements

Execute DML statements:

var rows = await db.Database.ExecuteSqlRawAsync(
    "UPDATE USERS SET BALANCE = BALANCE + 100 WHERE ID = {0}",
    1);

Query a scalar value (the ExecuteSqlRaw method returns the number of affected rows, which is not suitable for directly reading the result of a SELECT statement):

var count = db.Database
    .SqlQuery<int>($"SELECT COUNT(*) FROM USERS WHERE NAME = {"Alice"}")
    .Single();

Notice

ExecuteSqlRaw is suitable for INSERT, UPDATE, and DELETE statements. To query a scalar value, use SqlQuery<T>() or the equivalent API for your version.

Migrations and database creation

Code First migrations:

dotnet ef migrations add InitialCreate
dotnet ef database update

EnsureCreated:

await db.Database.EnsureCreatedAsync();

Value generation

The provider supports value generation for HiLo, identity, and GUID.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.UseOceanBaseHiLo("APP_HILO_SEQ");

    modelBuilder.Entity<User>(entity =>
    {
        entity.Property(x => x.Id)
            .UseOceanBaseIdentityColumn();
    });
}

Code First / Data type mapping

When you generate or synchronize a table schema based on an entity, the default type mapping is as follows (actual values may vary):

CLR type Default database type
int NUMBER(10)
long NUMBER(19)
short NUMBER(6)
byte NUMBER(3)
decimal DECIMAL(29,4) / NUMBER(p,s)
double FLOAT(49) / BINARY_DOUBLE
float BINARY_FLOAT
string Default VARCHAR2; NVARCHAR2 when Unicode is explicitly specified
DateTime DATE / TIMESTAMP
DateTimeOffset TIMESTAMP WITH TIME ZONE
byte[] BLOB / RAW
TimeSpan INTERVAL DAY TO SECOND

For fields such as amounts and decimals, we recommend that you explicitly configure HasColumnType("NUMBER(10,2)") or HasPrecision(10, 2) and ensure that the settings match the database table definition. If you have specific requirements for length, precision, or column type, explicitly configure them in the Fluent API or data annotations. Do not rely solely on the default mapping.

Considerations

1. Default string mapping

  • By default, strings are mapped to VARCHAR2.
  • Strings are mapped to NVARCHAR2 only when you explicitly configure IsUnicode(true).

2. decimal precision and scale

We recommend that you explicitly configure:

entity.Property(x => x.Balance)
    .HasColumnType("NUMBER(10,2)");

or:

entity.Property(x => x.Balance)
    .HasPrecision(10, 2);

3. NOT NULL string columns

The provider handles the issue of null values for string columns in newly added entities through the SaveChanges interceptor:

  • If a default value is specified, the model's default value is written first.
  • If no default value is specified, an empty string "" is written.

4. ExecuteSqlRaw and SELECT

The ExecuteSqlRaw method is suitable for executing INSERT, UPDATE, DELETE, and DDL statements. It is not suitable for using SELECT COUNT(*) to return a scalar value.

Correct example:

var count = db.Database
    .SqlQuery<int>($"SELECT COUNT(*) FROM USERS")
    .Single();

Summary

For EF Core 8.0 + OceanBase Oracle mode, the current provider supports the following main paths:

  • DbContext access
  • CRUD operations
  • Transactions
  • Parameterized queries
  • Raw SQL
  • Common LINQ translation
  • Migrations / EnsureCreated
  • Value generation
  • Scaffolding
  • Execution strategies

If your business requires JSON queries, more advanced LINQ translations, or EF Core 9 compatibility, verify them separately before integration.

Previous topic

Supported result set types
Last

Next topic

Use SqlSugar
Next
What is on this page
Install
Connection string
Get started
Example
Execute DML statements
Migrations and database creation
Value generation
Code First / Data type mapping
Considerations
1. Default string mapping
2. decimal precision and scale
3. NOT NULL string columns
4. ExecuteSqlRaw and SELECT
Summary