Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 无法更新备用密钥实体框架核心1.0_C#_Asp.net_Sql Server_Asp.net Mvc_Entity Framework - Fatal编程技术网

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

C# 无法更新备用密钥实体框架核心1.0,c#,asp.net,sql-server,asp.net-mvc,entity-framework,C#,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,我正在为我的新项目使用EntityFramework7或Core1.0。在products表中,ProductName列被设置为备用键(唯一约束)。问题是我无法更新数据库中的此列。编辑操作的代码如下所示: [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(ProductViewModel product, int id, IFormFile ProductImage) { try { i

我正在为我的新项目使用EntityFramework7或Core1.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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共IActionResult编辑(ProductViewModel产品,int id,IFormFile ProductImage)
{
尝试
{
if(ModelState.IsValid)
{
var Product=_products.Find(x=>x.ProductID==id).Single();
字符串tempName=Product.ProductName;//用于在名称更改时删除目录。
Product=\u mapper.Map(产品);
//如果已上载或删除新文件,则替换图像文件的代码
//如果产品名称已更改,请删除/更改目录
//去这里
//映射后再次插入id
Product.ProductID=id;
ProductImage=_images.Find(m=>m.ProductID==id).Single();
image.Hash=文件名;
形象。产品=产品;
image.Extension=FileExtension;
image.ProductID=Product.ProductID;
_产品。更新(产品);
_图片。更新(图片);
if(_products.SaveAll()&&&_images.SaveAll())
{
返回操作(“索引”);
}
}
}
捕获(例外情况除外)
{
_logger.LogDebug(例如消息);
投掷;
}
product.Categories=_Categories.GetAll().ToList();
返回视图(产品);
}

我已经调试完毕,一切正常,数据库中的所有其他属性正在更新,ProductName正在内存对象(而不是数据库)中更新,文件/文件夹正在被替换,甚至图像数据库表也在更新,但是,当更改产品名称时,SaveAll()if语句中的return语句不会执行,数据库中的这个特定列也不会更新。请帮忙

好的,我通过Entity Framework Core的Github找到了答案。答案如下:

EF Core当前不支持更改备用键的值。不过,我们确实有办法取消这一限制

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

资料来源:

无法更改设置为备用键的列

如果您只想在表中使用唯一索引,可以将
HasIndex
IsUnique
一起使用,例如:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>()
        .HasIndex(u => u.ProductName)
        .IsUnique();
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasIndex(u=>u.ProductName)
.IsUnique();
}
索引是许多数据存储中的常见概念。而他们 数据存储中的实现可能会有所不同,它们用于 基于一列(或一组列)的查找效率更高。通过 按照惯例,在每个属性(或属性集)中创建索引 属性),用作外键


阅读更多关于

的信息,我相信您可以简化代码@JuanCarlosOropeza DoneStill我不清楚错误在哪里。你能说得更具体一点吗,你有什么错误吗?你也可以试试
db.SaveChanges()而不是
\u products.Update(产品)@JuanCarlosOropeza mate没有错误。它只是不更新列,跳过
saveall
if中的redirect语句,并返回相同的编辑表单。关于
db.SaveChanges()
,我正在使用存储库模式和
SaveAll()
方法抽象
db.SaveChanges()。希望这有帮助。所以更新一些专栏,而其他的没有?在这种情况下,您应该检查您的类定义,看看是否有ReadOnly属性。我花了很长时间才弄明白。