Getting "NEXT VALUE FOR" for a oracle database sequence using EF Core 3.1

I'm writing a web API and trying to get sequence from the oracle database and create a method with help of this

var p = new Oracle.ManagedDataAccess.Client.OracleParameter("@result", Oracle.ManagedDataAccess.Client.OracleDbType.Int32);
            p.Direction = System.Data.ParameterDirection.Output;
            Context.Database.ExecuteSqlRaw("set @result = next value for Social_Media_Clip_Seq", p);
            var nextVal = (int)p.Value;

but facing error ORA-00922: missing or invalid option

Can anyone help me to find what is missing or is any other way to find out the solution Thanks in advance


Solution 1:

If your API needs to find nextval only to insert new records into database, maybe you can configure the model to use the sequence directly for generating the new Ids:

protected override void OnModelCreating(ModelBuilder modelBuilder) {
...
    modelBuilder.HasSequence<long>("SEQUENCE_DB_NAME");
    modelBuilder.Entity<C#TYPENAME>(entity =>
            {
                entity.Property(e => e.Id)
                    .HasColumnName("ID")
                    .ValueGeneratedOnAdd()
                    .UseHiLo("SEQUENCE_DB_NAME");
            });

Source: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implementation-entity-framework-core#the-hilo-algorithm-in-ef-core

However, my environment is EF Core 5.0.13, Oracle EntityFrameworkCore 5.21.5, Oracle database version is 19.3.