Question

I have created T4 template and when I save it I get the following errors: what could be the problem

the code is and the error is because of assembly c:\net\ex....

<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension="generated.cs" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Data.SqlServerCe" #>
<#@ assembly name="System.Data.Entity" #>

<#@ assembly name="C:\Net\Excesrise\Final\CodeGenWitht4Templates\bin\Debug\CodeGenWithT4Templates.exe" #>

<#@ assembly name="System.xml" #>

<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlServerCe" #>
<#@ import namespace="System.Data.Entity" #>
using System;

namespace Demo.Entities 
{    
  <#


                    var connectionString=@"data source=C:\Net\Excesrise\Final\CodeGenWitht4Templates\bin\Debug\CodeGenWithT4Templates.exe";

                        SqlCeConnection conn = new SqlCeConnection(connectionString);
            conn.Open(); 
            System.Data.DataTable schema = conn.GetSchema("TABLES"); 
            string selectQuery = "select * from @tableName"; 
            var command = new SqlCeCommand(selectQuery,conn); 
            var ad = new SqlCeDataAdapter(command); 
            System.Data.DataSet ds = new DataSet(); 
            string currentTableName = String.Empty;

                        //PushIndent("          ");
            foreach(System.Data.DataRow row in schema.Rows) 
            { 
                currentTableName = row["TABLE_NAME"].ToString();
                                currentTableName = currentTableName = currentTableName.Replace(" ", "");
                                currentTableName = currentTableName.Replace("[", "");
                                currentTableName = currentTableName.Replace("]", "");
            #> 
public class <#= currentTableName #>   {
<#                 
    command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString()); 
    ad.FillSchema(ds, SchemaType.Mapped, row["TABLE_NAME"].ToString());

    foreach (DataColumn dc in ds.Tables[0].Columns)
    {
        var typeName = dc.DataType.Name;
        var propName = dc.ColumnName.Replace(dc.ColumnName[0].ToString(), dc.ColumnName[0].ToString().ToLower());
      propName = propName.Replace(" ", "");

        WriteLine ( "  public " + typeName + " " + propName + " {get;set;}" );                 
    }                 #>
 }                

      <#    
      }  #>  
}

Error 1:

A namespace cannot directly contain members such as fields or methods C:\Net\Excesrise\Final\CodeGenWitht4Templates\ClassesFromDB.generated.cs

Error 2

Compiling transformation: Metadata file 'C:\Net\Excesrise\Final\CodeGenWitht4Templates\bin\Debug\CodeGenWithT4Templates.exe' could not be found C:\Net\Excesrise\Final\CodeGenWitht4Templates\ClassesFromDB.tt 1 1 CodeGenWithT4Templates

The Reason for the error i guess

<#@ assembly name="C:\Net\Excesrise\Final\CodeGenWitht4Templates\bin\Debug\CodeGenWithT4Templates.exe" #>

....

Was it helpful?

Solution

Second error: you don't need to reference your own exe. Because

  1. Assembly directive is for assemblies, not executables
  2. You use only standard classes
  3. This exe is built using the output of your T4, recursion ?)

Also Connection String to local database is not to your exe, it's like

 conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");

Rest of your code looks good. And Your first error is from a bad generated file, because it's like this

ErrorGeneratingOutput
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top