【发布时间】:2019-12-02 18:47:21
【问题描述】:
我正在使用 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”中的标识列输入显式值。
【问题讨论】:
-
您的配置说要使用 SQL server 的标识值,但代码在插入之前仍然为 PreFamFatId 赋值。你不能同时拥有这两种方式。如果你希望 SQL server 分配值,不要在代码中分配它,如果你希望你的代码生成它,不要将它配置为标识列。
标签: c# asp.net-mvc entity-framework ef-core-2.0