Is it possible to execute a SQL (TSQL or Linq To SQL) from within a T4 Template so that the schema information from a table can be used to customized the code generation?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes of course – you can execute any arbitrary valid .NET code in a T4 template – that template gets converted into a .NET class, after all, which then gets compiled and executed.
What’s your issue? What have you tried to do and how far have you come? Where are the roadblocks??
UPDATE:
I tried the following:
AdventureWorksDataAdventureWorksDataassemblyWith this setup in place, I can write a T4 template something like this:
What I do here is enumerate over the
Productsin the AdventureWorksLT sample database, and I grab all products withProductCategoryID == 5, using Linq.I then create a class which holds strings where the name of the string variable corresponds to the (sanitized) name of the product itself, and the string value contains some of the bits of information on that product in the database.
As you can see, you can fairly easily incorporate a Linq-to-SQL model and a LINQ query into your T4 template.