2

我们有一个项目,它使用实体框架来对数据库的数据进行增删改查。

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

当我尝试访问从数据库中检索数据的函数时,我收到一条错误消息,指出实体框架提供程序没有返回继承自 System.Data.Entity.Core.Common.DbProviderServices 的对象。

好的,暂时忘记错误:我的猜测是,负责处理通常从bin\debug\目录加载的实体框架访问的程序集现在正在从目录中某处的 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 流程临时存储程序集的地方。

4

1 回答 1

0

我不再使用 T4 来解决这个问题,而是使用将数据生成到文件中的控制台应用程序来解决它。这些文件被加载到 Mock 对象中。

于 2019-02-18T15:33:42.250 回答