Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - T4 generation issues

I'm trying to learn some C# and coming from a data background. Now I want to generate some SQL code based on a database. I'm working with the examples from https://damieng.com/blog/2009/11/06/multiple-outputs-from-t4-made-easy-revisited. When I download the solution from Github (https://github.com/DanielGasson/t4-examples) it works.

But I'm having some issues with this. I hope that somebody can help me a little because that I've tried many things and I get frustrated that this doesn't work.... So maybe somebody can get me back on track with some hints:

My issues:

Problem 1 - adding SQL connection I want to add some SQL to retieve some data that must be generated but now I have a C# project which gives problems when I try to add some SQL connection code like:

SqlConnection conn = new SqlConnection(connectionString); 
string selectQueryTables = "select * from [config].[TargetTables]"; 
SqlDataAdapter commandTables = new SqlDataAdapter(selectQueryTables,conn); 

This leads to this error: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?) T4CustomFileGeneration

I tried to add NuGet System.Data.SqlClient but that didn't solve anything.

What am I doing wrong?

Edit: After the tip off @Zer0 I've this:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ output extension="/" #>
<#@ template language="C#v3.5" hostspecific="True"#>
<#@ include file="Manager.ttinclude"#>
using System.Data.SqlClient;

<#
    var manager = new Manager(Host, GenerationEnvironment, true, "T4CustomFileGeneration")
    {
        OutputPath = Path.GetDirectoryName(Host.TemplateFile),
    };
#>

<#
    string connectionString = ""; 
    System.Data.SqlClient.SqlConnection conn = new SqlConnection(connectionString); 
....

So that's both solutions but the errors are still there:

error

Full code:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE"#>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Text"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ output extension="/" #>
<#@ template language="C#v3.5" hostspecific="True"#>
<#@ include file="Manager.ttinclude"#>
<#@ import namespace="System.Data.SqlClient"#>
<#@ import namespace="System.Data"#>

<#
    var manager = new Manager(Host, GenerationEnvironment, true, "T4CustomFileGeneration")
    {
        OutputPath = Path.GetDirectoryName(Host.TemplateFile),
    };
#>

<#
    string connectionString = ""; 
    System.Data.SqlClient.SqlConnection conn = new SqlConnection(connectionString); 
    string selectQueryTables = "select * from [config].[Tables]"; 
    SqlDataAdapter commandTables = new SqlDataAdapter(selectQueryTables,conn); 

    DataSet TablestoGenerate = new DataSet();
    commandTables.Fill(TablestoGenerate, "Tables");
    var manager = TemplateFileManager.Create(this);
#>

<# manager.StartBlock("FooStoredProc.sql", "Stored Procedures"); #>
CREATE PROC FOO ...
<# manager.EndBlock(); #>

<# manager.Process(); #>
question from:https://stackoverflow.com/questions/65891897/t4-generation-issues

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...