无法更新备用密钥实体框架核心1.0

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语句,也不会更新数据库中的特定列.请帮忙!

ar2*_*994 11

好的,我通过Entity Framework Core的Github找到了答案.这是答案:

EF Core目前不支持更改备用密钥的值.我们确实有#4073跟踪删除此限制.

顺便说一句,如果你想将它用作关系的目标键,它只需要是一个备用键.如果您只想要一个唯一索引,那么使用HasIndex()方法,而不是AlternateKey().可以更改唯一索引值.

资料来源: Github