EF 核心插入标识问题



我正在使用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);
}

这是我的控制器获取和发布方法:

[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' 中的标识列输入显式值。

这个错误是不言自明的。 您为"GmPreFamFatMkp"列提供了一个值,它是一个标识列(猜测自动增量(,并且"Identity_Insert"处于关闭状态。你可能想把它留给那个地方。不能为此列提供值。为其指定 null 或零值,并让 EF 和数据库找出正确的值。

最新更新