我正在尝试设置一个T4文件,该文件应该创建一个新的DBContext对象。配置是从.config文件加载的。
我使用的是DevartProvider,而不是默认的SqlServerProvider,它通常在项目中将CopyLocal设置为True时被引用。
在T4模板文件im中使用
<#@ assembly name="$(SolutionDir)SomePathDevart.Data.PostgreSql.Entity.dll" #>
但是当创建DBContext时,由于找不到dll,我得到了一个异常:
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.
我可以将DLL复制到执行T4的IDE文件夹中(C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\IDE),但由于此安装程序应该在多台计算机上运行,因此此解决方法是不可接受的。
如何在T4执行期间提供此程序集?
我最终使用了AppDomain.AssemblyResolve委托(请参阅如何在.NET中的运行时将文件夹添加到程序集搜索路径)。为了使以下代码完整,还应该提到在运行时更改默认app.config:
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();