[英]Using SSDT as source for T4 templates
I have a SQL Server Data Tools (SSDT) project that has a number of stored procedures for which I would like to generate the C# code to call them using T4. 我有一个SQL Server数据工具(SSDT)项目,它有许多存储过程,我想生成C#代码以使用T4调用它们。 Are there any existing examples for doing this?
这样做有没有现成的例子?
So far, I can create a function per proc, but I'd really like to be able to tap into the meta data SSDT creates so I can get the parameters, data types, and return values from it rather than doing string parsing. 到目前为止,我可以创建一个每个proc的函数,但我真的希望能够利用SSDT创建的元数据,这样我就可以从中获取参数,数据类型和返回值,而不是进行字符串解析。
COOL WITH A CAPITAL C! COOL与资本C! (but don't tell anyone who uses an ORM!)
(但不要告诉任何使用ORM的人!)
To get the datatypes etc make sure you grab the latest DacExtensions from the MS DacFx team: 要获取数据类型等,请确保从MS DacFx团队获取最新的DacExtensions:
https://github.com/Microsoft/DACExtensions https://github.com/Microsoft/DACExtensions
The new api (which incidentally is written using T4 templates) makes finding the info you need many many times simpler. 新的api(偶然使用T4模板编写)使得您需要的信息简单多了许多倍。
There should be enough information you need in this blog to get you going: 您应该在此博客中提供足够的信息来帮助您:
https://the.agilesql.club/Blogs/Ed-Elliott/DacFx-Create-tSQLt-Tests-From-A-Dacpac https://the.agilesql.club/Blogs/Ed-Elliott/DacFx-Create-tSQLt-Tests-From-A-Dacpac
The only difference is that you are creating C# and not T-SQL so you won't have to deal with the ScriptDom. 唯一的区别是您正在创建C#而不是T-SQL,因此您不必处理ScriptDom。
When you do this, please dump it on github it sounds like a really useful project. 当你这样做时,请把它转储到github上听起来像一个非常有用的项目。
To answer this question in the comments: 要在评论中回答这个问题 :
I can create methods with the correct parameters, but I'm struggling to find where the objects are in the model are that represent the content of a stored procedure.
我可以用正确的参数创建方法,但我很难找到模型中对象的位置,它们代表了存储过程的内容。 I need to know the columns returned by a SELECT statement in order to generate the return objects.
我需要知道SELECT语句返回的列,以便生成返回对象。 Any ideas?
有任何想法吗?
The referenced objects are provided by the TSqlProcedure.BodyDependencies relationship. 引用的对象由TSqlProcedure.BodyDependencies关系提供。 That will return objects referenced in the stored proc body, but won't tell you how they are used.
这将返回存储过程体中引用的对象,但不会告诉您如何使用它们。 The relational model doesn't try to embed this info as it doesn't help in deployment, but you can get it by querying the SQLDOM AST for the procedure.
关系模型不会尝试嵌入此信息,因为它在部署中没有帮助,但您可以通过查询SQLDOM AST来获取该过程。
The AST is a syntax tree defining the actual structure of the Procedure statement, including the structure of the procedur body. AST是定义Procedure语句的实际结构的语法树,包括程序体的结构。 What you need to do is:
你需要做的是:
A few notes / resources: 一些笔记/资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.