EF核心插入身份问题

时间:2019-12-02 18:47:21

标签: c# asp.net-mvc entity-framework ef-core-2.0

我正在使用EF Core 2.2.4,并且试图通过asp.net MVC页面插入数据。

这是我的存储库插入命令:

public int InsertFatorMarkup(int empId, GmPreFamFatMkp gmPreFamFatMkp, int usuId)
        {
            using (var transaction = _GFazContext.Database.BeginTransaction())
            {
                try
                {
                    var preFamFatId = EtapaInsertFator(empId, gmPreFamFatMkp, usuId);
                    transaction.Commit();
                    return preFamFatId;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }

        //Fernando Milanez 28/11/2019
        private int EtapaInsertFator(int empId, GmPreFamFatMkp gmPreFamFatMkp, int usuId)
        {
            gmPreFamFatMkp = RecargaDeDependenciasFator(empId, gmPreFamFatMkp);

            gmPreFamFatMkp.PreFamFatId = NextFatorMkpId(gmPreFamFatMkp.PreId, empId);
            gmPreFamFatMkp.RegrasNegocio(usuId);

            _GFazContext.GmPreFamFatMkp.Add(gmPreFamFatMkp);
            _GFazContext.SaveChanges();
            return gmPreFamFatMkp.PreFamFatId;
        }

        //Fernando Milanez 28/11/2019
        private GmPreFamFatMkp RecargaDeDependenciasFator(int empId, GmPreFamFatMkp gmPreFamFatMkp)
        {
            gmPreFamFatMkp.GmPreTab = GetById(empId, gmPreFamFatMkp.PreId);
            //gmPreFamFatMkp.GmPreTab = _GFazContext.GmPreTab
            //    .FirstOrDefault(r => r.EmpId == empId && r.PreId == gmPreFamFatMkp.PreId);

            return gmPreFamFatMkp;
        }

        //Fernando Milanez 28/11/2019
        private short NextFatorMkpId(int preId, int empId)
        {
            var max = _GFazContext.GmPreFamFatMkp
                .Where(r => r.PreId == preId)
                .Select(r => (int)r.PreFamFatId)
                .DefaultIfEmpty(0)
                .Max();

            return Convert.ToInt16(max + 1);
        }

这是我的控制器的Get和Post方法:

 [HttpGet]
        public IActionResult FamiliaMarkupInsert(int preId)
        {
            ViewBag.returnUrl = Request.Headers["Referer"].ToString();

            var model = new PreFamFatMkpModel();
            model.Form = new GmPreFamFatMkp() { PreId = preId };
            model.Form.GmPreTab = _gmPreTabRepositorio.GetById(_empId, preId, false);
            model.FamiliaModel = new FamiliaModel();
            GetDataCombo(ref model);

            return View(model);
        }

        //Fernando Milanez 29/11/2019
        [HttpPost]
        public IActionResult FamiliaMarkupInsert(PreFamFatMkpModel model)
        {
            ViewBag.returnUrl = Request.Headers["Referer"].ToString();
            if (ModelState.IsValid)
            {
                try
                {
                    int newPreFamFatId = _gmPreTabRepositorio.InsertFatorMarkup(_empId, model.Form, _usuId);
                    return RedirectToAction("TabelaPrecoTabs", new { id = model.Form.PreId, tabName= "Markup Por Família" });
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    if (ex.InnerException != null) ModelState.AddModelError("", ex.InnerException.Message);
                }
            }
            GetDataCombo(ref model);
            model.Form.GmPreTab = _gmPreTabRepositorio.GetById(_empId, model.Form.PreId);
            return View(model);
        }

这是我的配置类:

public class GmPreFamFatMkpConfigurations : IEntityTypeConfiguration<GmPreFamFatMkp>
    {
        public void Configure(EntityTypeBuilder<GmPreFamFatMkp> builder)
        {
            builder.HasKey(r => new { r.PreFamFatId });
            builder.Property(r => r.PreFamFatId).UseSqlServerIdentityColumn();

            //Monta Relacionamento 1-N - Colocar somente as dependencias, nesse caso depende da tabela de preço e do produto

            builder.HasOne(prepro => prepro.GmPreTab).WithMany(pretab => pretab.GmPreFamFatMkps).HasForeignKey(prepro => prepro.PreId);

            builder.HasOne(prefamfatmkp => prefamfatmkp.GmPreTab).WithMany(famtab => famtab.GmPreFamFatMkps).HasForeignKey(prefamfatmkp => prefamfatmkp.PreId);


        }
    }

最后,这是我的错误:

  

当IDENTITY_INSERT设置为OFF时,无法在表'GmPreFamFatMkp'中为标识列输入一个明确的值。

1 个答案:

答案 0 :(得分:1)

该错误很容易解释。您已为“ GmPreFamFatMkp”列提供了一个值,它是“身份”列(猜测自动增量),并且“ Identity_Insert”已关闭。您可能要这样离开。您无法为此列提供值。将其设置为零或零,然后让EF和数据库找出正确的值。