How can an Oracle database's identity be generated automatically using Entity Framework?

.net c# entity-framework ora-00001 oracle

Question

I'm having trouble utilizing the Oracle provider for Entity Framework (beta).

Id columns in our tables are configured in StoreGeneratedPattern to be Identity. I believed that every entry I added to the database would cause EF to carry out "underlying tasks," such as generating new identities and sequences. However, when I execute the following code to create a new record:

var comment = new Comment
{
    ComplaintId = _currentComplaintId,
    Content = CommentContent.Text,
    CreatedBy = CurrentUser.UserID,
    CreatedDate = DateTime.Now
};

context.Comments.AddObject(comment);
context.SaveChanges();

Still, an Exception throws, which is

{"ORA-00001: unique constraint (ADMINMGR.CONSTRAINT_COMMENT) violated"}

(CONSTRAINT_COMMENT is the constrain requires that comment identity must be unique.

How can I fix this?

I really appreciate it.

1
12
12/8/2011 4:05:17 PM

Accepted Answer

Simply put, StoreGeneratedPattern="Identity" instructs EF not to give a value in insert statements and that the value will be created DB-side on insert.

Oracle sequence creation is still required:

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

and an event that causes table inserts to utilize 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;
16
6/7/2012 11:50:37 AM

Popular Answer

It has been fixed in Oracle 12c.

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow