实体框架 4 - 获取生成的更新/插入 SQL
使用 EF4,是否可以获取生成的用于更新/插入的 SQL,而不是执行它......就像您可以在运行之前查看查询 SQL 一样。
原因是,我有一组执行 SQL 命令的辅助函数。例如...
Decrement<Category>("ProductCount", categoryID);
SetNull<Product>("CategoryID", productID);
生成...
UPDATE Categories
SET ProductCount = ProductCount - 1
WHERE CategoryID = @CategoryID;
UPDATE Products
SET CategoryID = NULL
WHERE CategoryID = @ProductID;
我通常每个操作运行多个命令,因此每次调用辅助函数时,都会生成并存储 SQL。当我调用 SaveChanges() 时,所有命令都会同时运行。
唯一的问题是 EF 在幕后单独运行其命令,然后我立即运行其他命令。将所有内容作为一个命令运行将是理想的选择。
With EF4, is it possible to get the generated SQL for Updates/Inserts rather than executing it... just like you can view the query SQL before it runs.
The reason is, I have a set of helper functions that execute SQL commands. For instance...
Decrement<Category>("ProductCount", categoryID);
SetNull<Product>("CategoryID", productID);
Which generates...
UPDATE Categories
SET ProductCount = ProductCount - 1
WHERE CategoryID = @CategoryID;
UPDATE Products
SET CategoryID = NULL
WHERE CategoryID = @ProductID;
I usually run several commands per operation, so with each helper function call, the SQL is generated and stored. When I call SaveChanges(), all of the commands are run at ONE time.
The only problem is that EF runs its commands separately behind the scenes, then I run the others right afterward. It would be ideal to run everything as one single command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在设计时使用 Sql Profiler 获得它,但我认为您的意思是您希望在运行时获得它。这是我找到的有关如何执行此操作的示例:
找到 这里。
You can get it at design time with Sql Profiler, but I think you're meaning that you want it at run-time. Here's an example I found on how to do that:
Found here.