如何通过Entity框架自动为Oracle数据库生成身份?

发布于 2024-10-20 22:01:04 字数 616 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终难愈 2024-10-27 22:01:04

StoreGeneratePattern="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;

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;
灯下孤影 2024-10-27 22:01:04

Oracle 12c已经解决了

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

Oracle 12c has resolved it

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SomeNumber { get; set; }
紫轩蝶泪 2024-10-27 22:01:04

另一种选择是:

按照 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.

远山浅 2024-10-27 22:01:04

我正在使用 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

城歌 2024-10-27 22:01:04

您无需记住所有这些 SQL,只需使用 Mig# 即可轻松完成,如下所示

        var schema = new DbSchema(ConnectionString, DbPlatform.Oracle12c);
        schema.Alter(db => db.CreateTable("TableName")
            .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
            ...);

:在此示例中,Id 列将具有 Mig# 自动生成的所需触发器和序列。

Instead of remember all of this SQL, you could easily do by using Mig# like this:

        var schema = new DbSchema(ConnectionString, DbPlatform.Oracle12c);
        schema.Alter(db => db.CreateTable("TableName")
            .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
            ...);

In this example, the Id column will have the required trigger and sequence generated by Mig# automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文