EF无法看到受影响的行

时间:2015-09-15 18:27:23

标签: c# entity-framework mariadb

当我尝试将数据插入表格时,我遇到了问题。我正在使用EF,MariaDB和不幸的#39;触发。当我尝试插入新数据时,EF抛出异常OptimisticConcurrencyException

这是我的触发器:

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

简单插入后显示异常:

var v = new Data
{
    Id = someObject.Id,
    Location = "text",
    Name = "Data_" + someObject.Id
};
mariaDb.Add(v);
mariaDb.SaveChanges();

例外:

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()实现 - RepositoryPattern:

    public virtual void SaveChanges()
    {
        DbContext.SaveChanges();
    }

有没有办法教EF触发器或我应该修改触发器?我需要一个带有自动增量主键的表,并且可以通过EF插入pk。如果我在id列上有一个只有autoincrement的表,那么EF会在SaveChanges()之后覆盖插入的id。

感谢您的任何建议。

0 个答案:

没有答案