Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot update alternate key entity framework core 1.0

I'm using entity framework 7 or core 1.0 for my new project. In the products table, ProductName column is set as an alternate key (unique constraint). The problem is that I'm unable to update this column in the database. The code for edit actions is as follows:

[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);
}

I've debugged through all of this and everything is working fine, all other properties are being updated in the database, the ProductName is being updated in memory objects (not the database), the files / folders are being replaced, even the images database table is being updated, but when the product name is changed the return statement inside the SaveAll() if statement isn't executed nor is this particular column in database being updated. Please help!

like image 922
ar27111994 Avatar asked Mar 25 '26 05:03

ar27111994


2 Answers

Ok I've found the answer through Entity Framework Core's Github. Here's the answer:

EF Core currently does not support changing the value of alternate keys. We do have #4073 tracking removing this restriction though.

BTW it only needs to be an alternate key if you want it to be used as the target key of a relationship. If you just want a unique index, then use the HasIndex() method, rather than AlternateKey() . Unique index values can be changed.

Source: Github

like image 162
ar27111994 Avatar answered Mar 27 '26 18:03

ar27111994


You can't change the column setted as an alternate key.

If you just want a unique index in your table you can use HasIndex with IsUnique, for example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>()
        .HasIndex(u => u.ProductName)
        .IsUnique();
}

Indexes are a common concept across many data stores. While their implementation in the data store may vary, they are used to make lookups based on a column (or set of columns) more efficient. By convention, an index is created in each property (or set of properties) that are used as a foreign key.

Read more about EF Core Indexes

like image 30
Denis Bubnov Avatar answered Mar 27 '26 17:03

Denis Bubnov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!