Problems using Entity Framework 6 and SQLite

前端 未结 4 1044
情深已故
情深已故 2020-11-28 20:21

I\'m trying to use Entity Framework with SQLite. I had issues integrating it into my main application, so I started a little test from scratch, exactly following the directi

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 20:51

    For anyone using the code based setup and still getting the same exception: I needed to additionally set the connection factory in kjbartel's answer.

    public class SQLiteConnectionFactory : IDbConnectionFactory
    {
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            return new SQLiteConnection(nameOrConnectionString);
        }
    }
    

    Then set the default connection factory in SQLiteConfiguration

    public class SQLiteConfiguration : DbConfiguration
    {        
        public SQLiteConfiguration()
        {
            SetDefaultConnectionFactory(new SQLiteConnectionFactory());
            SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
            SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
            Type t = Type.GetType( "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6");
            FieldInfo fi = t.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static);
            SetProviderServices("System.Data.SQLite", (DbProviderServices)fi.GetValue(null));
        }
    }
    

提交回复
热议问题