如何通过Entity框架自动为Oracle数据库生成身份?
我正在使用 Oracle 实体框架提供程序(测试版),但我遇到了问题。
我们的表有 Id 列,在 StoreGeneratePattern 中设置为 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:唯一约束(ADMINMGR.CONSTRAINT_COMMENT) 违反了”}
(CONSTRAINT_COMMENT 是要求注释标识的约束 必须是唯一的。
我该如何解决这个问题?
非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
StoreGeneratePattern="Identity" 只是告诉 EF 该值将在插入时在数据库端生成,并且不应在插入语句中提供值。
您仍然需要在 Oracle 中创建一个序列:
以及一个触发器来使表插入使用它:
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:
and a trigger to make table inserts use it:
Oracle 12c已经解决了
Oracle 12c has resolved it
另一种选择是:
按照 Alextansc 描述的方式创建序列。
创建一个使用 MySequence.nextval 作为主键的存储过程。
将此模型的“插入”映射到您的存储过程,它就可以工作了!
我已经使用数据库优先方法对此进行了测试。
使用数据库优先映射到存储过程非常简单。转到 edmx 文件并右键单击要映射到存储过程的模型。单击“存储过程映射”。页面底部的对话框提供了三个下拉菜单,用于将插入、更新和删除映射到存储过程。
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.
我正在使用 Oracle ODP.NET、托管驱动程序和实体框架 6。我使用代码优先方法创建了表,但由于主键为空,无法添加任何记录。
解决方案是授予我的用户以下两项:
“创建序列” 和
“创建触发器”
权限并重新创建架构。
在包管理控制台中使用 -verbose 标志后我意识到了这一点
I am using Oracle ODP.NET, Managed driver and Entity Framework 6. I created my tables using the code-first approach but wasn't able to add any records due to a null primary key.
The solution was to grant my user both:
'CREATE SEQUENCE' and
'CREATE TRIGGER'
permissions and re-create the schema.
I realized this after using the -verbose flag in the package management console
您无需记住所有这些 SQL,只需使用 Mig# 即可轻松完成,如下所示
:在此示例中,
Id
列将具有 Mig# 自动生成的所需触发器和序列。Instead of remember all of this SQL, you could easily do by using Mig# like this:
In this example, the
Id
column will have the required trigger and sequence generated by Mig# automatically.