bubibubi / JetEntityFrameworkProvider

Microsoft Access (Jet) Entity Framework provider
89 stars 26 forks source link

Set JetEntityFrameworkProvider dynamically with ConnectionString #57

Closed wojo1206 closed 3 years ago

wojo1206 commented 3 years ago

VS2017 Professional, VB.NET project for .NET Framework 4.0. I got following error messages during build: Type JetConnectionFactory is not defined. I also added JetEntityFrameworkProvider.dll to GAC. I installedJetEntityFrameworkProvider v6.1.4 from NuGet and I confirm that the library is referenced and exists. I am trying to setup DbContext configuration dynamically like:

...
Imports JetEntityFrameworkProvider

Public Class ORMConfiguration
    Inherits DbConfiguration
    Public Sub DbContextConfiguration()
        Dim con1 = New OleDb.OleDbConnection(ORMGlobal.ConnectionString)
        SetProviderServices("JetEntityFrameworkProvider", Core.Common.DbProviderServices.GetProviderServices(con1))
        SetDefaultConnectionFactory(New JetConnectionFactory())
    End Sub
End Class

<DbConfigurationType(GetType(ORMConfiguration))>
Partial Public Class ORM
    Inherits DbContext

    Public Sub New()
        MyBase.New(ORMGlobal.ConnectionString)
    End Sub
End Class
...

My app.config:

...
  <entityFramework>
    <providers>
      <provider invariantName="JetEntityFrameworkProvider" type="JetEntityFrameworkProvider.JetProviderServices, JetEntityFrameworkProvider"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="JetEntityFrameworkProvider"/>
      <add invariant="JetEntityFrameworkProvider" name="Jet Entity Framework Provider" description="Jet Entity Framework Provider"
        type="JetEntityFrameworkProvider.JetProviderFactory, JetEntityFrameworkProvider"/>
    </DbProviderFactories>
  </system.data>
...

How can I dynamically select JetEntityFrameworkProvider provider for DbContext and set connection string properly?

wojo1206 commented 3 years ago

Answering myself ;) JetEntityFrameworkProvider v6.0.0.0 does not work with .NET Framework version below 4.5!