Generate identity for an Oracle database through Entity Framework using an exisiting stored procedure

China☆狼群 提交于 2019-12-08 21:38:44

问题


How to automatically generate identity for an Oracle database through 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.

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:


1) Create sequence in Oracle

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

2)Create property

   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)Getting Sequence

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


来源:https://stackoverflow.com/questions/8232997/generate-identity-for-an-oracle-database-through-entity-framework-using-an-exisi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!