I have a issue when I try to inserts data to the table. I am using a EF, MariaDB and 'unfortunatelly' trigger. When I try to insert new data, EF throws exception OptimisticConcurrencyException
.
Here is my trigger:
CREATE TRIGGER AutoIncrdata BEFORE INSERT ON data
FOR EACH ROW BEGIN
DECLARE new_id integer;
SET new_id = (select max(id_data) + 1 from data);
IF (NEW.id_data < 1) then
SET NEW.id_data = COALESCE(new_id, 1 );
END IF;
END
The exception shows after simple insert:
var v = new Data
{
Id = someObject.Id,
Location = "text",
Name = "Data_" + someObject.Id
};
mariaDb.Add(v);
mariaDb.SaveChanges();
Exception:
InnerException {"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions."} System.Exception {System.Data.Entity.Core.OptimisticConcurrencyException}
SaveChanges() implementations - RepositoryPattern:
public virtual void SaveChanges()
{
DbContext.SaveChanges();
}
Is there any way how to teach EF the trigger or I should modify the trigger? I need a table with autoincrement primary key and with possibility of inserting pk via EF. If I have a table just with autoincrement on id column so the EF overrides inserted id after SaveChanges()
.
Thanks for any advice.