EF6, SQLite won't work without App.config
The minimum needed to make the constructor with connection string working is a custom IProviderInvariantName
, IDbDependencyResolver
and DbConfiguration
:
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()); }}
Now this should work:
var context = new MyContext("Data Source = mytest.db; Version = 3;");var entities = context.MyEntities.ToList();
Update: For NET4.0 you would also need a custom 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; }}
and add
if (type == typeof(IDbProviderFactoryResolver)) return SQLiteDbProviderFactoryResolver.Instance;
to the SQLiteDbDependencyResolver.GetService
method implementation.