1

I'm trying to set up a T4 file that should create a new DBContext object. The configuration is loaded from a .config File.

I'm using a DevartProvider instead of the default SqlServerProvider, which is normally referenced with CopyLocal set to True within a project.

Within the T4 template file im using

<#@ assembly name="$(SolutionDir)\SomePath\Devart.Data.PostgreSql.Entity.dll" #>

but when creating the DBContext I get an Exception because the dll is not found:

Additional information: The Entity Framework provider type 'Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity' registered in the application config file for the ADO.NET provider with invariant name 'Devart.Data.PostgreSql' could not be loaded Make sure that the assembly-qualified name is used and that the assembly is available to the running application.

I could copy the DLL to the IDE folder where the T4 gets executed (C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE), but since this setup should run on multiple machines this workaround would be unacceptable.

How can I make this assembly available during T4 execution?

Dresel
  • 2,375
  • 1
  • 28
  • 44
  • Consider register the assembly to GAC, and then reference it? – Karata Oct 25 '14 at 13:43
  • Well registering within the GAC would also lead to additional steps to make this work which have to be replicated on each development machine which I try to avoid. There is also another DLL that must be referenced (where the DatabaseInitializer is defined) so a code based solution within the T4 would be my preferred choice. – Dresel Oct 25 '14 at 13:50

1 Answers1

1

I ended up using the AppDomain.AssemblyResolve delegate (see How to add folder to assembly search path at runtime in .NET). For complettenes of the following code, Change default app.config at runtime should also be mentioned:

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += (sender, eventArgs) =>
{
    string lookupPath = @"PathToTheDllFolder";

    var assemblyname = new AssemblyName(eventArgs.Name).Name;
    var assemblyFileName = Path.Combine(lookupPath, assemblyname + ".dll");
    var assembly = Assembly.LoadFrom(assemblyFileName);

    return assembly;
};

AppConfig.Change(@"PathToConfig.config");

var entities = new MyEntities();
Community
  • 1
  • 1
Dresel
  • 2,375
  • 1
  • 28
  • 44