如何自动生成实体通过框架的一个Oracle数据库的身份? [英] How to automatically generate identity for an Oracle database through Entity framework?

查看:234
本文介绍了如何自动生成实体通过框架的一个Oracle数据库的身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Oracle提供的实体框架(测试版),而我面临的一个问题。



我们的表有标识列,这被设置为认同StoreGeneratedPattern。我认为,EF会自动做背后的作品,如创建序列,并获得每个I添加到该表记录的新身份。但是,当我运行代码来添加一个新的记录,如:

  VAR评论=新评论
{
ComplaintId = _currentComplaintId,
含量= CommentContent.Text,
CreatedBy = CurrentUser.UserID,
CreatedDate = DateTime.Now
};

context.Comments.AddObject(注解);
context.SaveChanges();

这是异常还是抛出,这是



<块引用>

{ORA-00001:唯一约束(ADMINMGR.CONSTRAINT_COMMENT)
违反}



(CONSTRAINT_COMMENT是约束要求注释身份
必须是唯一的。




我要如何解决这个问题?



非常感谢你!


解决方案


StoreGeneratedPattern =身份只是告诉EF该值会。在INSERT生成的DB-一面,它不应该在提供插入语句的值



您还需要在Oracle中创建一个序列:



<预类=郎-SQL prettyprint-覆盖> 创建序列ComplaintIdSequence MINVALUE 1 MAXVALUE 9999999从1开始递增1;

和触发器,使表插入使用它:



<预类=郎-SQL prettyprint-覆盖 > 创建或评论插入之前重新触发CommplaintIdTrigger
的每一行
开始
如果:new.ComplaintId为null,则选择ComplaintIdSequence.nextval到:从new.ComplaintId双;
ENDIF;
端;


I'm using Oracle provider for Entity framework (beta), and I'm facing a problem.

Our tables have Id columns, which are set to be Identity in StoreGeneratedPattern. I thought that EF will automatically do "underlying works", such as create sequences, and get new identity for each record I add to the table. But when I run code to add a new record, such as:

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

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

an Exception still throws, which is

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

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

How do I solve this?

Thank you very much!

解决方案

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数据库的身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆