C# T4模板失败,因为它们尝试从visual studio公用目录加载程序集
我们有一个项目,它使用实体框架对数据库中的数据进行CRUD 我现在正试图编写一个T4模板,它使用EF访问以代码形式生成testdata,比如C# T4模板失败,因为它们尝试从visual studio公用目录加载程序集,c#,.net,entity-framework,t4,assembly-loading,C#,.net,Entity Framework,T4,Assembly Loading,我们有一个项目,它使用实体框架对数据库中的数据进行CRUD 我现在正试图编写一个T4模板,它使用EF访问以代码形式生成testdata,比如var users=new List{new User{…},new User{…} 当我尝试访问从数据库检索数据的函数时,我得到一个错误,即实体框架提供程序没有返回从System.data.Entity.Core.Common.DbProviderServices继承的对象 好的,暂时忘记这个错误:我猜负责实体框架访问的程序集通常从bin\debug\目录
var users=new List{new User{…},new User{…}代码>
当我尝试访问从数据库检索数据的函数时,我得到一个错误,即实体框架提供程序没有返回从System.data.Entity.Core.Common.DbProviderServices继承的对象
好的,暂时忘记这个错误:我猜负责实体框架访问的程序集通常从bin\debug\
目录加载,现在正从Visual Studio工作目录加载,该目录位于C:\Program Files(x86)\
目录的某个地方。这会导致找到不同的程序集。我不想为所有需要的程序集使用绝对路径,因为这会给我的同事带来问题,他们的电脑上可能有不同的配置
是否可以直接从$(SolutionDir)运行模板
编辑
我想我更进一步了:下面的脚本现在要求在App.config中输入连接字符串,而该字符串又不在Visual Studio的工作目录中
<#@ template debug="true" hostspecific="false" language="C#" compileOptions="-P $(SolutionDir)Wur.Epros.Core" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Data.Common" #>
<#@ assembly name="System.Data.DataSetExtensions" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.IO.Compression" #>
<#@ assembly name="System.Runtime.Serialization" #>
<#@ assembly name="System.Security" #>
<#@ assembly name="System.ServiceModel" #>
<#@ assembly name="System.Transactions" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System.ComponentModel.DataAnnotations" #>
<#@ assembly name="$(SolutionDir)packages\CuttingEdge.Conditions.1.2.0.0\lib\NET35\CuttingEdge.Conditions.dll" #>
<#@ assembly name="$(SolutionDir)packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll" #>
<#@ assembly name="$(SolutionDir)packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll" #>
<#@ assembly name="$(SolutionDir)packages\NLog.4.5.11\lib\net45\NLog.dll" #>
<#@ assembly name="$(SolutionDir)packages\Oracle.ManagedDataAccess.12.2.1100\lib\net40\Oracle.ManagedDataAccess.dll" #>
<#@ assembly name="$(SolutionDir)packages\Oracle.ManagedDataAccess.EntityFramework.12.2.20190115\lib\net45\Oracle.ManagedDataAccess.EntityFramework.dll" #>
<#@ assembly name="$(SolutionDir)packages\structuremap.2.6.3\lib\StructureMap.dll" #>
<#@ assembly name="$(SolutionDir)Wur.Epros.Core\bin\debug\Wur.Epros.Core.dll" #>
<#@ assembly name="$(SolutionDir)Wur.Epros.Core.Tests\bin\debug\Wur.Epros.Core.Tests.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Wur.Epros.Core.Domain" #>
<#@ import namespace="Wur.Epros.Core.Infrastructure" #>
<#@ import namespace="Wur.Epros.Core.Tests.Mock" #>
<#@ import namespace="System.Data.Entity" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="StructureMap" #>
<#@ import namespace="Wur.Epros.Core.Infrastructure.StructureMap" #>
<#@ import namespace="Oracle.ManagedDataAccess.Client" #>
<#@ output extension=".cs" #>
// Test
<#
try
{
ObjectFactory.Configure(x =>
{
x.AddRegistry<CoreRegistry>();
x.Scan(scan =>
{
scan.WithDefaultConventions();
scan.LookForRegistries();
scan.TheCallingAssembly();
});
});
Wur.Epros.Core.Infrastructure.Environment.InitEnvironment();
DataClass.GenerateData<EPROS_SETUP>(DataClass.GetData<EPROS_SETUP>());
}
catch(Exception ex)
{
#>
/* <#= ex.Message #>
<#= ex.InnerException == null ? "null" : ex.InnerException.Message #> */
<#
}
#>
//试验
{
x、 AddRegistry();
x、 扫描(扫描=>
{
scan.WithDefaultConventions();
scan.LookForRegistries();
扫描。卡入总成();
});
});
Wur.Epros.Core.Infrastructure.Environment.InitEnvironment();
DataClass.GenerateData(DataClass.GetData());
}
捕获(例外情况除外)
{
#>
/*
*/
编辑2
离解决这个问题又近了一步,但仍然没有。我添加了以下代码来初始化上面的脚本(也编辑了一些)
使用系统配置;
使用System.Data.Entity;
命名空间Wur.Epros.Core.Infrastructure
{
///
///环境变量
///
公共静态课堂环境
{
公共静态环境()
{
DbConfiguration=new MyConf();
SetConfiguration(conf);
}
}
公共类MyConf:DbConfiguration
{
公共MyConf()
{
SetDefaultConnectionFactory(新的System.Data.Entity.Infrastructure.LocalDbConnectionFactory(“v13.0”);
SetProviderServices(“System.Data.SqlClient”、System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetProviderServices(“Oracle.ManagedDataAccess.Client”、Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance);
}
}
}
我现在得到以下错误:
[A]Oracle.ManagedDataAccess.Client.OracleConnection无法强制转换为[B]Oracle.ManagedDataAccess.Client.OracleConnection。类型A源自位置“c:\program files(x86)的上下文“Default”中的“Oracle.ManagedDataAccess,Version=4.122.1.0,Culture=neutral,PublicKeyToken=89b483f429c47342”\oracle developer tools for vs2017\odp.net\managed\common\oracle.manageddataaccess.dll”。类型B源于位置C:\Users\\AppData\Local\assembly\dl3\15K0NR5V.AGY\O2O039L7.GXW\137c2a2f\00141e2f\u 86d5d201\oracle.Manag的上下文“LoadFrom”中的“oracle.manageddataaccess,Version=4.122.1edDataAccess.dll.
它尝试从两个不同的位置加载程序集。我猜第二个位置是T4进程临时存储程序集的位置。我不再使用T4查找此问题,而是使用控制台应用程序将数据生成到文件中解决了此问题。文件加载到模拟对象中。无法通过配置初始化问题。它在de devenv工作目录中搜索App.config。无法将其动态添加到内存配置(只读)。可以将其添加到实体框架DBContext,但仍会搜索App.config。我想最好的解决方案是能够更改TextTemplatingFileGenerator运行的工作目录。遇到此帖子:。仍不工作忘记提及。在上述解决方案中,EF的connectionstring使用“var context”解决=新的EprosEntities(“”;’)似乎有效。删除了我的评论。我想知道这个问题的答案。任何建议都是welcomeEF&Oracle?如果您在这件事上有任何选择,请使用另一种方法。使用IDataReaders进行选择,并对其他所有内容使用命令。它可能是基本的,但经过了测试。
using System.Configuration;
using System.Data.Entity;
namespace Wur.Epros.Core.Infrastructure
{
/// <summary>
/// Environment variables
/// </summary>
public static class Environment
{
public static void InitEnvironment()
{
DbConfiguration conf = new MyConf();
DbConfiguration.SetConfiguration(conf);
}
}
public class MyConf : DbConfiguration
{
public MyConf()
{
SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v13.0"));
SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetProviderServices("Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance);
}
}
}