entity-framework-core - 如何获取 EF-Core MigrationSqlGenerator 中 Generate 函数生成的 SQL 命令?
问题描述
使用 EF-Core 处理迁移时的自定义 SQL 命令。是否可以获取由覆盖Generate
函数生成的 SQL 命令?
protected override void Generate(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
{
base.Generate(operation, model, builder);
var generatedSQL = ?; // <-- how can I get this SQL command?
}
解决方案
MigrationSqlGenerator
生成MigrationCommand
可以从MigrationCommandListBuilder
viaGetCommandList
方法获得的实例列表。SQL 命令文本包含CommandText
在MigrationCommand
.
因此,如果您确定基本调用会生成一个命令,那么您可以在之后从该列表中获取最后一个命令:
var generatedSQL = builder.GetCommandList().Last().CommandText;
没有任何假设的更通用的方法可能是这样的:
var commands = builder.GetCommandList();
var start = commands.Count;
base.Generate(operation, model, builder);
for (int i = start; i < commands.Count; i++)
{
var generatedSQL = commands[i].CommandText;
}
推荐阅读
- python - 创建唯一项目的 df 和每个唯一项目的另一列的最大值
- python - 有没有办法比较存储的值和用户的输入以删除 django 视图中的重复项?
- facebook - 无法将 Facebook 评论插件添加到网站
- python - ImportError:pycurl:libcurl 链接时 ssl 后端(schannel)不包括编译时 ssl 后端(openssl)
- javascript - 如何在 Formik 的选项标签的值中传递对象?
- python - 通过实现 Python、RabbitMQ、NodeJS 向网站显示消息
- r - 在 R 的数字数据框中使用函数 na_ma
- windows - 我可以指定一个不是正方形或矩形的特定非客户区吗?
- sql - 我想减少 SQL SELECT SUM 重复聚合
- amazon-web-services - 通过 boto3 将公共访问策略附加到 S3 存储桶“MalformedPolicy 错误”不断抬头