使用 entity framework 时加载了多个 Sybase dll

Multiple Sybase dlls are loaded when using entity framework

我正在使用 SQL Anywhere 17 和 entity framework 6。当我尝试从数据库中获取一些数据时出现此异常。

An exception of type 'System.InvalidCastException' occurred in 

EntityFramework.dll but was not handled in user code    
Additional information: [A]Sap.Data.SQLAnywhere.SAConnection
cannot be cast to [B]Sap.Data.SQLAnywhere.SAConnection. Type A originates 
from 'Sap.Data.SQLAnywhere.v4.5, Version=17.0.0.10624, Culture=neutral, 
PublicKeyToken=f222fc4333e0d400' in the context 'Default' at 
location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Sap.Data.SQLAnywhere.v4.5
\v4.0_17.0.0.10624__f222fc4333e0d400\Sap.Data.SQLAnywhere.v4.5.dll'. Type B 
originates from 'Sap.Data.SQLAnywhere.EF6, Version=17.0.0.10624, 
Culture=neutral, PublicKeyToken=f222fc4333e0d400' in the context 'Default' at 
location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Sap.Data.SQLAnywhere.EF6
\v4.0_17.0.0.10624__f222fc4333e0d400\Sap.Data.SQLAnywhere.EF6.dll'.

这是我的代码,

public class SybaseConfiguration : DbConfiguration
{
    public SybaseConfiguration()
    {
        SetProviderServices("Sap.Data.SQLAnywhere", SAProviderServices.Instance);
    }
}

[DbConfigurationType(typeof(SybaseConfiguration))]
public partial class SomeDatabaseContext : DbContext
{ ... }


// Calling code,
using (var context = new SomeDatabaseContext(connectionString)
    context.GetSomeRandomTable.ToList() // I get exception here.

当我创建上下文对象时,仅加载 Sap.Data.SQLAnywehre.EF6。但是当我调用 GetSomeRandomTable 时它会加载 Sap.Data.SQLAnywhere.v4.5(它不应该),

调试器说 Sap.Data.SQLAnywhere.v4.5 和 Sap.Data.SQLAnywhere.EF6 都已加载。

请注意,配置文件中没有任何内容。我是代码库配置。

请确保您在 App.config 中没有为此设置的配置部分,因为 据我所知,EF 无论如何都会加载它。

我就是这个,而且效果很好:

public class SampleDbConfiguration : DbConfiguration
{
    public SampleDbConfiguration()
    {
        // Set provider
        SetProvider();
    }

    /// <summary>
    /// Set Sql-Aynwhere as the current entity framework provider
    /// </summary>
    private void SetProvider()
    {
        // not required...
        this.SetDefaultConnectionFactory(new SampleDBConnectionFactory());

        this.SetProviderServices("iAnywhere.Data.SQLAnywhere", iAnywhere.Data.SQLAnywhere.SAProviderServices.Instance);
        this.SetProviderFactory("iAnywhere.Data.SQLAnywhere", iAnywhere.Data.SQLAnywhere.SAFactory.Instance);
    }
}

只需将其替换为您的不变名称,因为我们使用的是 sybase 16。另外 确保您没有引用其他 dll,也没有通过(例如)LoadAssembly.

加载它

编辑

这不是必需的,但您可以根据需要创建自己的 DbConnection。

internal class SampleDBConnectionFactory : IDbConnectionFactory
{
    /// <summary>
    /// Create SA-DB Connection
    /// </summary>
    /// <param name="nameOrConnectionString">Name of complete connection string</param>
    /// <returns>Instance of an SQL-Connection to a sybase db</returns>
    public System.Data.Common.DbConnection CreateConnection(string nameOrConnectionString)
    {
        SAConnection connection = new SAConnection(ConnectionManager.GetConnectionString(nameOrConnectionString ?? "Default"));

        return connection;
    }
}

希望对您有所帮助。