I have a table that has unique index over 3 columns and 'item_no' is one of those. Here is my code:

    var poItem = _poitemRepository.Table.FirstOrDefault(p => p.po_number == Data.VendinvdQry.po_number &&
             p.rel_numb == Data.VendinvdQry.rel_numb &&
             p.item_no == vendinvlnQry.item_no);

    if(poItem != null)
    {
        //_poitemRepository.Reload(poItem);
        poItem.inv_ext_cost += xAdjVal;
        _poitemRepository.Update(poItem);
    }

It does not matter what I do here it is throwing error. I also tried Reload Method and even that throws the same error. What I am not understanding is it works for same po_number and rel_number and item_no(10, 20, 30, 40) and it breaks for the same po_number, rel_number and but for item_no = 50.

Here is the entity mapping file:

public partial class PoitemMap : NopEntityTypeConfiguration<Poitem>
{
    public PoitemMap()
    {
        this.ToTable("poitem");
        this.HasKey(c => new { c.po_number, c.rel_numb, c.item_no });

        this.Property(u => u.polin_serial).HasColumnName("polin_serial");
        this.Property(u => u.locationx).HasColumnName("location");
        this.Property(u => u.job_no).HasColumnName("job_no");
        this.Property(u => u.acct_no).HasColumnName("acct_no");
        this.Property(u => u.commodity_code).HasColumnName("commodity_code");
        this.Property(u => u.po_number).HasColumnName("po_number");
        this.Property(u => u.item_no).HasColumnName("item_no").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(u => u.vend_code).HasColumnName("vend_code");
        this.Property(u => u.vend_site).HasColumnName("vend_site");
        this.Property(u => u.vend_name).HasColumnName("vend_name");
        this.Property(u => u.product_req).HasColumnName("product_req");

            this.Property(u => u.part_code).HasColumnName("part_code");
            this.Property(u => u.v_part_number).HasColumnName("v_part_number");
            //.......
        }
    }

Here is my Update Method:

public virtual int Update(T entity)
{
    int affectedRows = 0;
    try
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        affectedRows = _context.SaveChanges();
    }
    catch (DbEntityValidationException dbEx)
    {
        var msg = GetErrorMessage(dbEx);
        var fail = new Exception(msg, dbEx);
        throw fail;
    }

    return affectedRows;
}

The table does not have foreign key, primary key except the unique index I mentioned. I am not understanding what is causing this error.

0

There are 0 best solutions below