没有App.config,EF6,SQLite将无法运行

问题描述 投票:15回答:1

我正在尝试制作一个插件,它将使用EF6.1和SQLite用于我无法更改App.config的应用程序,因此需要在代码中设置所有配置和连接字符串。

我发现这个答案看起来很有前途的Problems using Entity Framework 6 and SQLite

所以现在我有一个这样的配置类:

public class CollectionDbConfiguration : DbConfiguration
{
    public CollectionDbConfiguration()
    {
        SetProviderServices("System.Data.SQLite"(DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
        SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
        SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);                 
    }
}

我已经确认在第一次创建上下文之前会遇到这种情况并且所有这些都返回值。

我的上下文看起来像这样

public class MyContext : DbContext
{
    public MyContext(string connectionString)
    :   base(connectionString) { }

    public DbSet<MyEntity> MyEntities { get; set; }      
}

我有一些代码称之为:

var context = new MyContext("Data Source = mytest.db; Version = 3;");
var entities = context.MyEntities.ToList();

当我尝试运行代码时,看起来上下文假设连接字符串是针对SQL Server的,因为它给了我:

不支持关键字:'版本'。

如果我删除它然后我得到一个错误,它无法连接,它显然尝试连接到SQL Server数据库。

我尝试通过添加构造函数传递SQLite连接:

public MyContext(DbConnection connection) 
    : base(connection, contextOwnsConnection: true)
{ }

并用它调用它:

var context = new MyContext(new SQLiteConnection("Data Source = mytest.db; Version = 3;"));
var entities = context.MyEntities.ToList();

但后来我得到了错误:

无法为“System.Data.SQLite.SQLiteConnection”类型的连接确定DbProviderFactory类型。确保在应用程序配置中安装或注册了ADO.NET提供程序。

所以我做了一个工厂解析器并注册了

public class FactoryResolver : IDbProviderFactoryResolver
{
    public DbProviderFactory ResolveProviderFactory(DbConnection connection)
    {
        if (connection.GetType() == typeof(SQLiteConnection))
        {
            return SQLiteFactory.Instance;
        }

        if (connection.GetType() == typeof(EntityConnection))
        {
            return SQLiteProviderFactory.Instance;
        }

        return null;
    }
}

并补充说:

SetProviderFactoryResolver(new FactoryResolver());

但现在我明白了:

没有为ADO.NET提供程序找到具有不变名称“System.Data.SQLite.EF6”的实体框架提供程序。确保提供程序已在应用程序配置文件的“entityFramework”部分中注册。有关更多信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882

我现在已经在这里炒了两天而且我已经没想完了。

c# sqlite entity-framework-6
1个回答
13
投票

使连接字符串工作的构造函数所需的最小值是自定义IProviderInvariantNameIDbDependencyResolverDbConfiguration

public class SQLiteProviderInvariantName : IProviderInvariantName
{
    public static readonly SQLiteProviderInvariantName Instance = new SQLiteProviderInvariantName();
    private SQLiteProviderInvariantName() { }
    public const string ProviderName = "System.Data.SQLite.EF6";
    public string Name { get { return ProviderName; } }
}

class SQLiteDbDependencyResolver : IDbDependencyResolver
{
    public object GetService(Type type, object key)
    {
        if (type == typeof(IProviderInvariantName)) return SQLiteProviderInvariantName.Instance;
        if (type == typeof(DbProviderFactory)) return SQLiteProviderFactory.Instance;
        return SQLiteProviderFactory.Instance.GetService(type);
    }

    public IEnumerable<object> GetServices(Type type, object key)
    {
        var service = GetService(type, key);
        if (service != null) yield return service;
    }
}

class SQLiteDbConfiguration : DbConfiguration
{
    public SQLiteDbConfiguration()
    {
        AddDependencyResolver(new SQLiteDbDependencyResolver());
    }
}

现在这应该工作:

var context = new MyContext("Data Source = mytest.db; Version = 3;");
var entities = context.MyEntities.ToList();

更新:对于NET4.0,您还需要一个自定义IDbProviderFactoryResolver

class SQLiteDbProviderFactoryResolver : IDbProviderFactoryResolver
{
    public static readonly SQLiteDbProviderFactoryResolver Instance = new SQLiteDbProviderFactoryResolver();
    private SQLiteDbProviderFactoryResolver() { }
    public DbProviderFactory ResolveProviderFactory(DbConnection connection)
    {
        if (connection is SQLiteConnection) return SQLiteProviderFactory.Instance;
        if (connection is EntityConnection) return EntityProviderFactory.Instance;
        return null;
    }
}

并添加

if (type == typeof(IDbProviderFactoryResolver)) return SQLiteDbProviderFactoryResolver.Instance;

SQLiteDbDependencyResolver.GetService方法实现。

© www.soinside.com 2019 - 2024. All rights reserved.