How to get Oracle next sequence value in .NET Entity Framework?

.net, entity-framework, nextval, oracle11g, sequence

Solution

The way I got around this was select a new value from the sequence using a raw SQL query.

i.e.

decimal nextPK = context.Database.SqlQuery<decimal>("SELECT my_primary_key_seq.nextval FROM dual").First();

And then just assign this value to the new object before adding it to the context.

Problem

I have a web api post method that inserts a new row in my table in my Oracle database. I'm having trouble with the primary key of the table which is a sequence value. How do I do `my_primary_key_seq.nextval` in Entity Framework? Currently this code works but it will violate PK unique constrain when a new row is inserted via our old .net webform which uses the sequence next value as the next primary key. ``` decimal nextPK = context.FORMPPs.OrderByDescending(p => p.PPID).FirstOrDefault().PPID + 1; item.PPID = nextPK; context.FORMPPs.Add(item); int result = context.SaveChanges(); ```

Original source

Related problems