使用实体框架时,会加载多个Sybase dll [英] Multiple Sybase dlls are loaded when using entity framework

查看:685
本文介绍了使用实体框架时,会加载多个Sybase dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQL Anywhere 17和实体框架6.当我尝试从数据库中获取一些数据时,我得到这个例外。

I am using SQL Anywhere 17 and entity framework 6. When I try to get some data from the database I get this exception.

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(它不应该),

When I make context object, only Sap.Data.SQLAnywehre.EF6 is loaded. But when I call GetSomeRandomTable it loads Sap.Data.SQLAnywhere.v4.5 (which it should not),

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

Debugger says that both Sap.Data.SQLAnywhere.v4.5 and Sap.Data.SQLAnywhere.EF6 are loaded.

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

Note that there is nothing in the config file. I am code base configuration.

推荐答案

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

Make sure you have no configuration section for this in the App.config, because as far as i know, EF will load it in anyway.

我是这样,它的工作原理:

I'm this and it works perfectly:

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 加载它。

Just replace it with your invariant name, cause we are using sybase 16. Also make sure, that you don't reference the other dll and do not load it via (for example) LoadAssembly.

编辑

这不是必需的,但是如果你想要的话可​​以创建自己的DbConnection to。

This is not needed, but allows you to create your own DbConnection if you want to.

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

希望thius有帮助。

Hope thius helps.

这篇关于使用实体框架时,会加载多个Sybase dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆