MySQL EF6 'System.Data.EntityClient' 未在机器中注册

标签 mysql vb.net winforms entity-framework-6

好的,我有一个使用 MySQL 创建器自动创建的工作项目。我可以毫无问题地加载表单和添加/上/删除数据。

但是当我将项目更改为类库并尝试引用上下文时,出现以下错误。

具有不变名称“MySql.Data.MySqlClient;”的 ADO.NET 提供程序未在机器或应用程序配置文件中注册,或无法加载。有关详细信息,请参阅内部异常。

    System.ArgumentException was unhandled
  HResult=-2147024809
  Message=The ADO.NET provider with invariant name 'System.Data.EntityClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.<GetService>b__0(ArgumentException e, String n)
       at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func`3 handleFailedLookup)
       at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
       at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
       at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
       at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
       at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
       at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
       at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
       at System.Data.Entity.QueryableExtensions.Load(IQueryable source)
       at TvLinksDataEf6.DbWorker.GetSettings() in W:\Projects\TvLinks\TvLinkDataEf6\DBWorker.vb:line 663
       at TvLinksControls.SettingsPanel.LoadSettings() in W:\Projects\TvLinks\TVLinksControls\Panels\SettingsPanel.vb:line 43
       at TvLinks.Main.SettingsMenu_Menu_Click() in W:\Projects\TvLinks\TvLinks\Main.vb:line 212
       at TvLinksControls.MenuButton.Button_Click(Object sender, EventArgs e) in W:\Projects\TvLinks\TVLinksControls\MenuButton.vb:line 20
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at TvLinks.My.MyApplication.Main(String[] Args) in :line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2147024809
       Message=Unable to find the requested .Net Framework Data Provider.  It may not be installed.
       Source=System.Data
       StackTrace:
            at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
            at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func`3 handleFailedLookup)
       InnerException: 

我的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <connectionStrings><add name="TvSeriesDB" connectionString="metadata=res://TvLinksDataEf6/TvSeriesDB.csdl|res://TvLinksDataEf6/TvSeriesDB.ssdl|res://TvLinksDataEf6/TvSeriesDB.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;port=3306;server=localhost;user id=root;password=xxx;database=TvLinksMetro&quot;" providerName="System.Data.EntityClient"/></connectionStrings>
</configuration>

这是导致问题的代码

    _localDs = New TvSeriesDB()

    _localDs.settings.Load()

上下文:

Partial Public Class TvSeriesDB
    Inherits DbContext

    Public Sub New()
        MyBase.New("TvSeriesDB")
        MyBase.Configuration.LazyLoadingEnabled = False
    End Sub

    Public Sub New(conn As String)
        MyBase.New(conn)
        MyBase.Configuration.LazyLoadingEnabled = False
    End Sub

    Public Sub New(existingConnection As DbConnection, contextOwnsConnection As Boolean)
        MyBase.New(existingConnection, contextOwnsConnection)
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        MyBase.OnModelCreating(modelBuilder)
        modelBuilder.Entity(Of settings)().MapToStoredProcedures()
        modelBuilder.Entity(Of episodes)().MapToStoredProcedures()
        modelBuilder.Entity(Of rssfeeds)().MapToStoredProcedures()
        modelBuilder.Entity(Of tvseries)().MapToStoredProcedures()
    End Sub

    Public Overridable Property episodes() As DbSet(Of episodes)
    Public Overridable Property rssfeeds() As DbSet(Of rssfeeds)
    Public Overridable Property settings() As DbSet(Of settings)
    Public Overridable Property tvseries() As DbSet(Of tvseries)

End Class

再次请注意,我引用的是一个使用 create MySQL winforms 项目创建的工作项目。

机器上安装的提供程序列表。

Odbc Data Provider .Net Framework Data Provider for Odbc System.Data.Odbc System.Data.Odbc.OdbcFactory, System.Data, Version=4.0.0.0

OleDb Data Provider .Net Framework Data Provider for OleDb System.Data.OleDb System.Data.OleDb.OleDbFactory, System.Data, Version=4.0.0.0

OracleClient Data Provider .Net Framework Data Provider for Oracle System.Data.OracleClient System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=4.0.0.0

SqlClient Data Provider .Net Framework Data Provider for SqlServer System.Data.SqlClient System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0

Microsoft SQL Server Compact Data Provider 4.0 .NET Framework Data Provider for Microsoft SQL Server Compact System.Data.SqlServerCe.4.0 System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0

MySQL Data Provider .Net Framework Data Provider for MySQL MySql.Data.MySqlClient MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.6.0

这来自 Machine.config。

  <system.data>
    <DbProviderFactories>
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </DbProviderFactories>
  </system.data>

I created another simple solution with 2 winforms projects the first with my Model1 working no errors. Here I am referencing project1 and trying to get the same dataset from my Model1 and again same error.

我用 2 个 winforms 项目创建了另一个简单的解决方案,第一个是我的 Model1 没有错误。 在这里,我引用了 project1 并试图从我的 Model1 中获取相同的数据集,但同样的错误。 另外快速更新我下载了所有最新的 MySQl 连接器和 MySQl for Visual Studio 甚至将服务器更新到最新版本。在我这样做之后,我创建了这个小测试解决方案。

最佳答案

问题是定义了多个提供者:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>

删除包含引用的 SQLClient:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
    </providers>
  </entityFramework>

这仅在像我正在做的那样从二级项目调用时受到影响。

关于MySQL EF6 'System.Data.EntityClient' 未在机器中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37637803/

相关文章:

java - Java中如何将ArrayList数据类型插入MySQL数据库

python - 使用 python 在没有唯一标识符的情况下抓取 html

mysql - 如何使用数据库mysql和后台 worker vb.net将数据插入到客户端上的服务器?

c# - 如何获取控件的屏幕截图? DrawToBitmap 不工作

c# - Interop.Powerpoint : How to clear of undo (stack)

c# - 项目资源和本地资源图片的区别

php - 查询元素列表

mysql left join subselect

vb.net - 必须以自己的类型作为参数继承

javascript - Document.getElementById ('Button' ).click();不触发代码隐藏