How to automatically generate identity for an Oracle database through Entity framework? How to automatically generate identity for an Oracle database through Entity framework? oracle oracle

How to automatically generate identity for an Oracle database through Entity framework?


StoreGeneratedPattern="Identity" simply tells EF that the value will be generated DB-side on insert, and that it shouldn't supply a value in insert statements.

You still need to create a sequence in Oracle:

create sequence ComplaintIdSequence minvalue 1 maxvalue 9999999 start with 1 increment by 1;

and a trigger to make table inserts use it:

create or replace trigger CommplaintIdTrigger  before insert on comment for each row begin   if :new.ComplaintId is null then select ComplaintIdSequence.nextval into :new.ComplaintId from dual;   endif; end;


Oracle 12c has resolved it

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]public int SomeNumber { get; set; }


Another option would be:

Create a sequence the way Alextansc described.Create a stored procedure that uses MySequence.nextval as it's primary key.

Map 'insert' for this model to your stored procedure and it works!

I've tested this using database first approach.

Using database first mapping to a stored procedure is pretty simple. Go to your edmx file and right click the model you want to map to a stored procedure. Click "stored procedure mappings." The dialog at the bottom of the page gives you three drop down menus for mapping insert, update, and delete to stored procedures.