通过实体框架使用现有的存储过程为Oracle数据库生成身份 [英] Generate identity for an Oracle database through Entity Framework using an exisiting stored procedure

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

问题描述

如何通过Entity Framework自动生成Oracle数据库的身份?

How to automatically generate identity for an Oracle database through Entity Framework?

我有一个可以调用并生成不在上下文中的列的函数我通过Entity Framework显式调用存储过程吗?我正在使用存储库模式。

I have a function that I could call and generate the column which is not in the context how do I explicitly call the stored procedure through Entity Framework? I am using a repository pattern.

随机数生成器插入记录(我通过UDF获取主键,并将其传递给要插入的实体) / p>

Random Number generator to insert a record (where I get the primary key through a UDF and pass this to the entity to insert).

推荐答案

1)在Oracle中创建序列

 CREATE SEQUENCE dummy_test_seq
  MINVALUE 1
  MAXVALUE 999999999999999999999999999
  START WITH 1
  INCREMENT BY 1;

2)创建属性

   sealed public class CommonUtilities
    {
      #region Sequences
       public static int DummyTestSeq
        {
         get
          {              
            using (Entities ctx = new Entities()) 
             { 
               return Convert.ToInt32(ctx.Database.SqlQuery<decimal>("SELECT dummy_test_seq.NEXTVAL FROM DUAL").ToList().Single()); 
              }  
            }
         }
    #endregion
}

3)获取序列

   public int InsertTable1()
    {
      using (Entities ctx = new Entities())
        {
            ctx.tabel1.Add(new tabel1()
            {
                SEQ = CommonUtilities.DummyTestSeq,
                Date= DateTime.Now
            });
            return ctx.SaveChanges();
        }
     }

这篇关于通过实体框架使用现有的存储过程为Oracle数据库生成身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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