c# - 如何通过 Entity 框架自动为 Oracle 数据库生成标识?

标签 c# .net oracle entity-framework ora-00001

我正在为 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/

相关文章:

c# - 使 [IsOneWay=true] WCF 服务异步与在客户端使用任务调用同步方法之间是否存在显着差异?

c# - SQL查询结果到变量?

c# - 使用C#生成正态分布图

c# - 如何在 Emgu 中将 Mat 裁剪成 ROI(OpenCV for C#)

.net - 如何浏览 Websphere MQ 消息而不删除它?

c# - SMTP 中的传递通知

sql - 确定 BLOB 的高度和宽度

sql - 除 Oracle 之外的 RDBMS 中的 CONNECT BY 或分层查询

c# - 两个集合中的任何交集

c# - 如何使 Controller 名称连字符 "-"分隔?