T4模板失败,因为它们尝试从Visual Studio公共目录加载程序集

时间:2019-02-16 10:05:14

标签: c# .net entity-framework t4 assembly-loading

我们有一个项目,该项目使用实体框架对数据库中的数据进行CRUD。

我现在正在尝试编写一个T4模板,该模板使用EF访问来生成诸如var users = new List<User>{ new User {...}, new User {...} };之类的代码形式的测试数据。

当我尝试访问从数据库检索数据的函数时,出现错误消息,说明实体框架提供程序未返回继承自System.Data.Entity.Core.Common.DbProviderServices的对象。

>

好吧,暂时忽略一下错误:我的猜测是,现在通常从bin\debug\目录中加载的负责Entity Framework Access的程序集正在从某个位置的Visual Studio工作目录中加载在C:\Program Files (x86)\目录中。这将导致找到不同的程序集。我不想为所有需要的程序集使用绝对路径,因为这会对我的同事造成麻烦,这些同事可能在其PC上有不同的配置。

是否可以直接从$(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 #>  */


<#
    }
#>

编辑2

距离解决此问题又迈出了一步,但仍然没有完成。我添加了以下代码来初始化上面的脚本(也进行了一些编辑)。

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);
        }
    }
}

我现在收到以下错误:

[A]Oracle.ManagedDataAccess.Client.OracleConnection cannot be cast to [B]Oracle.ManagedDataAccess.Client.OracleConnection. Type A originates from 'Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' in the context 'Default' at location 'c:\program files (x86)\oracle developer tools for vs2017\odp.net\managed\common\oracle.manageddataaccess.dll'. Type B originates from 'Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' in the context 'LoadFrom' at location 'C:\Users\<userId>\AppData\Local\assembly\dl3\15K0NR5V.AGY\O2O039L7.GXW\137c2a2f\00141e2f_86d5d201\Oracle.ManagedDataAccess.dll'.

它尝试从2个不同的位置加载程序集。我猜第二个位置是T4流程临时存储程序集的位置。

0 个答案:

没有答案
相关问题