我映射的使用下面的代码存储过程,我的模型:实体框架MySQL的存储过程
modelBuilder.Entity<Account>()
.MapToStoredProcedures(p => p.Insert(sp => sp.HasName("sp_InsertAccount"))
.Update(sp => sp.HasName("sp_UpdateAccount"))
.Delete(sp => sp.HasName("sp_DeleteAccount"))
);
但是当我添加记录使用:
bank.AccountsMoves.Add(entity);
它抛出错误:
An unhandled exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll
Additional information: 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.
存储过程的sql语句为:
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_InsertAccountMove`(
IN type int(11),
IN account_id int(11),
IN accountant_id int(11),
IN amount int(11),
IN date datetime
)
BEGIN
INSERT INTO `bank`.`accounts_moves`
(`type`, `account_id`, `accountant_id`, `amount`, `date`)
VALUES (type, account_id,
(SELECT id FROM accountant
Where name = SUBSTRING_INDEX(USER() ,'@',1))
, amount, NOW());
END
我认为MySQL存储过程不返回受影响的行
我使用EF 6.0的数量时,MySQL 5.7
有谁知道如何解决这个错误的正确方法?
为什么使用实体框架在所有如果你只是要调用存储过程?也许你应该考虑像Dapper –
@DavidL这样轻的东西这是一种方法来重定向正常的CUD语句[存储过程](https://msdn.microsoft.com/en-us/data/dn468673.aspx?f=255&MSPPError = -2147217396)。阅读将通过常规的LINQ语句完成。 –
我不熟悉Mysql语法,但sproc应该返回新插入行的生成键值(如果存在任何生成的键值)。计算字段的值(如果有的话)。 –