实体框架 - MySQL - 存储过程 [英] Entity Framework - MySQL - stored procedure

查看:83
本文介绍了实体框架 - MySQL - 存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用以下代码将存储过程映射到我的模型:

I mapped stored procedure to my model using following code:

modelBuilder.Entity<Account>()
            .MapToStoredProcedures(p => p.Insert(sp => sp.HasName("sp_InsertAccount"))
            .Update(sp => sp.HasName("sp_UpdateAccount"))
            .Delete(sp => sp.HasName("sp_DeleteAccount"))
            );



但是当我使用以下命令添加记录时:

but when i add record using:

bank.AccountsMoves.Add(entity);



它抛出错误:

it throws error:


EntityFramework.dll中发生了'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'类型的未处理异常

An unhandled exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll

其他信息:存储更新,插入或删除语句影响了意外的行数(0)。实体可能已被加载或删除实体。请参阅"link"  for
有关理解和处理乐观并发异常的信息。

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 "link" for information on understanding and handling optimistic concurrency exceptions.



存储过程的sql语句是:

the sql statement for stored procedure is:

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存储过程不会返回受影响的行数

i think that mysql stored procedure does not return the number of affected lines


我使用的是EF 6.0,mysql 5.7

I am using EF 6.0, mysql 5.7


有谁知道如何以正确的方式解决这个错误?

Does anyone know how to fix this error the right way?

推荐答案

我认为mysql存储过程不会返回受影响的行数。

您需要在EF之外运行T-SQL,并使用任何可以使用的工具从MySQL数据库管理工具的T-SQL执行窗格运行T-SQL。

You need to run the T-SQL  outside of EF with whatever tool that will allow you to run the T-SQL from an T-SQL execution  pane from a MySQL DB Management tool.

附加信息:存储更新,插入或删除语句影响了意外的数字行(0)。

EF发出的异常消息意味着执行了T-SQL,但是执行了T-SQL受影响的零行(没有发生)T-SQL执行不起作用,应插入1行或更多行,但没有发生。

That message being put out on an exception by EF means that T-SQL was executed, but execution of the T-SQL affected zero rows (nothing happened) the T-SQL execution did not work, and 1 or more rows should have been inserted and it didn't happen.


这篇关于实体框架 - MySQL - 存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆