Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF核心插入标识问题_C#_Asp.net Mvc_Entity Framework_Ef Core 2.0 - Fatal编程技术网

C# EF核心插入标识问题

C# EF核心插入标识问题,c#,asp.net-mvc,entity-framework,ef-core-2.0,C#,Asp.net Mvc,Entity Framework,Ef Core 2.0,我使用的是EFCore2.2.4,我试图通过asp.NETMVC页面插入数据 以下是“我的存储库插入”命令: public int InsertFatorMarkup(int empId, GmPreFamFatMkp gmPreFamFatMkp, int usuId) { using (var transaction = _GFazContext.Database.BeginTransaction()) {

我使用的是EFCore2.2.4,我试图通过asp.NETMVC页面插入数据

以下是“我的存储库插入”命令:

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


        }
    }
公共类gmprefatmkpcConfiguration:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
HasKey(r=>new{r.PreFamFatId});
属性(r=>r.PreFamFatId).UseSqlServerIdentityColumn();
//Monta Relacionamento 1-N-科罗卡尔·萨门特作为家属,在生产前需要一张桌子
builder.HasOne(prepo=>prepo.GmPreTab).WithMany(pretab=>pretab.GmPreFamFatMkps).HasForeignKey(prepo=>prepo.PreId);
HasOne(prefamfatmkp=>prefamfatmkp.GmPreTab).WithMany(famtab=>famtab.GmPreFamFatMkps).HasForeignKey(prefamfatmkp=>prefamfatmkp.PreId);
}
}
最后,我的错误是:

当identity_INSERT设置为OFF时,无法为表“GmPreFamFatMkp”中的identity列输入显式值


这个错误是不言自明的。您已经为列“GmPreFamFatMkp”提供了一个值,它是一个标识列(猜测自动递增),并且“Identity_Insert”处于禁用状态。你可能想让它这样。无法为此列提供值。为其指定null或零值,让EF和数据库计算出正确的值。

您的配置要求使用SQL server的标识值,但代码在插入前仍为PreFamFatId赋值。你不可能两全其美;如果希望SQL server分配该值,请不要在代码中分配该值;如果希望代码生成该值,请不要将其配置为标识列。我已删除了NextFatorMarkup方法,它将调用。之后,插入工作正常。非常感谢。