ar2*_*994 2 c# sql-server asp.net asp.net-mvc entity-framework
我正在为我的新项目使用实体框架7或核心1.0.在products表中,ProductName列被设置为备用键(唯一约束).问题是我无法在数据库中更新此列.编辑操作的代码如下:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(ProductViewModel product, int id, IFormFile ProductImage)
{
try
{
if (ModelState.IsValid)
{
var Product = _products.Find(x => x.ProductID == id).Single();
string tempName = Product.ProductName; //for deleting directory if name has been changed.
Product = _mapper.Map<Product>(product);
//code to replace image file if new file has been uploaded OR
//delete / change directory if the product name has been changed
//goes here
//Insert id again after mapping
Product.ProductID = id;
ProductImage image = _images.Find(m => m.ProductID == id).Single();
image.Hash = FileName;
image.Product = Product;
image.Extension = FileExtension;
image.ProductID = Product.ProductID;
_products.Update(Product);
_images.Update(image);
if (_products.SaveAll() && _images.SaveAll())
{
return RedirectToAction("Index");
}
}
}
catch (Exception ex)
{
_logger.LogDebug(ex.Message);
throw;
}
product.Categories = _categories.GetAll().ToList();
return View(product);
}
Run Code Online (Sandbox Code Playgroud)
我通过所有这些进行了调试,一切正常,所有其他属性都在数据库中更新,ProductName正在内存对象(而不是数据库)中更新,文件/文件夹正在被替换,甚至是图像数据库正在更新表,但是当更改产品名称时,不会执行SaveAll() if语句中的return语句,也不会更新数据库中的特定列.请帮忙!