实体框架 4 - 获取生成的更新/插入 SQL

发布于 2024-10-16 14:17:21 字数 580 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别想她 2024-10-23 14:17:21

您可以在设计时使用 Sql Profiler 获得它,但我认为您的意思是您希望在运行时获得它。这是我找到的有关如何执行此操作的示例:

public static void WriteGeneratedSql(EntityCommand cmd)
{
     cmd.Prepare();

     IServiceProvider isp = (IServiceProvider)EntityProviderFactory.Instance;

     DbProviderServices mps = (DbProviderServices)isp.GetService(typeof(DbProviderServices));

     EntityCommandDefinition definition = (EntityCommandDefinition)mps.CreateCommandDefinition(cmd);

     int commandId = 1;

     foreach (string commandText in definition.MappedCommands)
     {
          Console.WriteLine("Generated Command {0}:", commandId);
          commandId++;
          Console.WriteLine(commandText);
     }
}

找到 这里

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:

public static void WriteGeneratedSql(EntityCommand cmd)
{
     cmd.Prepare();

     IServiceProvider isp = (IServiceProvider)EntityProviderFactory.Instance;

     DbProviderServices mps = (DbProviderServices)isp.GetService(typeof(DbProviderServices));

     EntityCommandDefinition definition = (EntityCommandDefinition)mps.CreateCommandDefinition(cmd);

     int commandId = 1;

     foreach (string commandText in definition.MappedCommands)
     {
          Console.WriteLine("Generated Command {0}:", commandId);
          commandId++;
          Console.WriteLine(commandText);
     }
}

Found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文