简体   繁体   English

EF Core 插入标识问题

[英]EF Core Insert Identity Issues

I'm using EF Core 2.2.4 and I'm trying to insert datas through an asp.net MVC page.我正在使用 EF Core 2.2.4 并且我正在尝试通过 asp.net MVC 页面插入数据。

Here's my repository insert command:这是我的存储库插入命令:

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);
        }

Here's my controller Get and Post methods:这是我的控制器 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);
        }

Here's my configuration class:这是我的配置类:

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);


        }
    }

And finally, heres my error:最后,这是我的错误:

Unable to enter an explicit value for the identity column in table 'GmPreFamFatMkp' when IDENTITY_INSERT is set to OFF.当 IDENTITY_INSERT 设置为 OFF 时,无法为表“GmPreFamFatMkp”中的标识列输入显式值。

The error is pretty self explanatory.该错误是不言自明的。 You've provided a value for the column 'GmPreFamFatMkp' and it is an Identity column (guessing auto-increment) and "Identity_Insert" is off.您已经为“GmPreFamFatMkp”列提供了一个值,它是一个身份列(猜测自动递增)并且“Identity_Insert”已关闭。 You probably want to leave it that way.你可能想就这样离开它。 You can't provide a value for this column.您不能为此列提供值。 Give it null or zero value and let EF and the database figure out the correct value.给它空值或零值,让 EF 和数据库找出正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM