【问题标题】:While inserting data into the oracle database code automatically generate value 0 for id using MVC with entity framework在将数据插入到 oracle 数据库代码中时,使用带有实体框架的 MVC 自动为 id 生成值 0
【发布时间】:2017-12-11 09:13:20
【问题描述】:

在将数据插入 Oracle 数据库代码时,使用 MVC 和实体框架自动为 id 生成值 0。如何解决这个问题。

public ActionResult AddNewNode(AddNode model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (Nspira_DBContext entity = new Nspira_DBContext())
                    {
                        TBL_ACCESSRIGHTS hierarchyDetail = new TBL_ACCESSRIGHTS()
                        {

                            NAME = model.NodeName,
                            PID = model.ParentName,
                        };

                        entity.TBL_ACCESSRIGHTS.Add(hierarchyDetail);
                        entity.SaveChanges();
                    }
                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return Json(new { success = false }, JsonRequestBehavior.AllowGet);
        }

我的表有ID,NAME和PID列。在数据库中插入查询意味着它生成了序列号。通过代码插入数据意味着它不创建序列。它自动取值为0。如何解决这个问题。

如何解决这个问题。请任何人帮助我。

【问题讨论】:

    标签: c# asp.net-mvc-4 oracle11g


    【解决方案1】:
     if (ModelState.IsValid)
                   {
                       using (Nspira_DBContext entity = new Nspira_DBContext())
                       {
    
                           int objid = entity.TBL_ACCESSRIGHTS.Max(p => p.ID);
                           TBL_ACCESSRIGHTS hierarchyDetail = new TBL_ACCESSRIGHTS()
                           {
                               ID = objid + 1,
                              NAME = model.NodeName,
                               PID = model.ParentName,
                           };
    
                           entity.TBL_ACCESSRIGHTS.Add(hierarchyDetail);
                           entity.SaveChanges();
                       }
                       return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                   }
               }
    

    请试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      相关资源
      最近更新 更多