1

I have the same code in a t4 template and in a unit test. Both reference the same assemblies: Unit test executes succefully, t4 return error: Running transformation: System.MissingMethodException: Method not found: 'System.Data.Entity.DbSet`1

T4 Template:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll" #>
<#@ assembly name="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" #>
<#@ assembly name="C:\VSProjects\VS.CodeGen\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll" #>
<#@ assembly name="C:\VSProjects\VS.CodeGen\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll" #>
<#@ assembly name="C:\VSProjects\VS.CodeGen\VS.CodeGen.Source\bin\Debug\VS.CodeGen.Source.dll" #>

<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="VS.CodeGen.Source.Models" #>
<#@ output extension=".cs" #>




<#

            var connectionString = "Data Source=MyComputer;Initial Catalog=GenSource;Integrated Security=True;MultipleActiveResultSets=True";
            using (var ctx = new GenSourceContext(connectionString))
            {
                var dsp = ctx.Dsps;
            }

#>

Test Code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using VS.CodeGen.Source.Models;


namespace VS.CodeGen.Test
{
    [TestClass]
    public class TestT4
    {
        [TestMethod]
        public void TestMethod1()
        {

            var connectionString = "Data Source=MyComputer;Initial Catalog=GenSource;Integrated Security=True;MultipleActiveResultSets=True";
            using (var ctx = new GenSourceContext(connectionString))
            {
                var dsp = ctx.Dsps;
            }

        }

    }
}

What's causing the error?

Thank you!

Debra
  • 94
  • 1
  • 10
  • Shouldn't the code inside the T4 template be inside a method inside a class? – Bryan Woodford Aug 26 '16 at 15:36
  • @BryanWoodford T4 templates control logic is written in control blocks - fragments of program code, this differs from regular object-oriented c# programming. Writing code in a T4 template: [https://msdn.microsoft.com/en-us/library/bb126478.aspx](https://msdn.microsoft.com/en-us/library/bb126478.aspx) – Debra Aug 29 '16 at 08:03
  • I mean, the code above will simply write a file with "var connectionString = ....; using (...) outside of a method and class, which won't compile. – Bryan Woodford Aug 30 '16 at 07:23
  • The code above (if it would transform as it does with the texttransform.exe command line tool) would produce an empty file. the "var connectionString=..." is part of the control logic not the output of the template. The output of a template can be text which does not need to compile. Thanks. – Debra Sep 06 '16 at 18:13
  • Try adding the path to the DLL directly, for example: `<#@ assembly name="$(SolutionDir)...\...\CodeGen.Source.dll" #>` and see if that runs. Looks like the issue is with the T4 template and the `DbContext`. – Bryan Woodford Sep 07 '16 at 11:41
  • As you can see, the template already includes the full path for referenced assemblies. – Debra Sep 14 '16 at 12:46
  • [This](http://stackoverflow.com/a/38927981) might help you. – Bryan Woodford Sep 16 '16 at 07:53

0 Answers0