System.data.SQLite entity framework code first programmatic providername specification

穿精又带淫゛_ 提交于 2019-12-12 12:29:20

问题


I've spent a while on this now and have only found a workaround solution that I'd rather not do...

I have a context as shown below. Is there a programmatic way to specify the database to connect to via the constructor, and get it to use the System.Data.SQLite entity framework provider to connect to a SQLite database? This is working via the app.config (with a connectionstring called "Context"), but not via any programmatic way I can find of supplying it. I have tried using an entity connectionstring builder and that produces the following string:

provider=System.Data.SQLite;provider connection string='data source="datafile.db"'

When the context is first queried with this string I get a message "Keyword not supported: 'provider'."

   public class Context : DbContext
    {
        public IDbSet<Test> Tests { get; set; }

        public Context()
            : base("Context")
        {
        }
    }

*Edit.

I may have solved this by implementing my own connectionfactory:

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

Then setting:

        Database.DefaultConnectionFactory = new ConnectionFactory();

However, it seems like there should be a built in way to do this, and also one that does not involve overriding the global default connection factory.

来源:https://stackoverflow.com/questions/19025177/system-data-sqlite-entity-framework-code-first-programmatic-providername-specifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!