我正在为 Entity Framework (测试版)使用 Oracle 提供程序,但我遇到了一个问题。
我们的表有Id列,在StoreGeneratedPattern中设置为Identity。我认为 EF 会自动执行“基础工作”,例如创建序列,并为我添加到表中的每条记录获取新标识。但是当我运行代码来添加一条新记录时,例如:
var comment = new Comment
{
ComplaintId = _currentComplaintId,
Content = CommentContent.Text,
CreatedBy = CurrentUser.UserID,
CreatedDate = DateTime.Now
};
context.Comments.AddObject(comment);
context.SaveChanges();
异常仍然抛出,这是
{"ORA-00001: unique constraint (ADMINMGR.CONSTRAINT_COMMENT) violated"}
(CONSTRAINT_COMMENT is the constrain requires that comment identity must be unique.
我该如何解决?
非常感谢!
最佳答案
StoreGeneratedPattern="Identity"只是告诉 EF 该值将在插入时在数据库端生成,并且它不应在插入语句中提供值。
您仍然需要在 Oracle 中创建一个序列:
create sequence ComplaintIdSequence minvalue 1 maxvalue 9999999 start with 1 increment by 1;
和一个使用它来插入表格的触发器:
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;
关于c# - 如何通过 Entity 框架自动为 Oracle 数据库生成标识?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227962/