好的,我有一个由MySQL创建者自动创建的工作项目。我可以加载表单并添加/ Up / Del数据没有问题。
但是当我将项目更改为类库并尝试引用上下文时,它会给我以下错误。
ADO.NET提供程序具有不变名称' MySql.Data.MySqlClient;'要么未在计算机或应用程序配置文件中注册,要么无法加载。有关详细信息,请参阅内部异常。
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="port=3306;server=localhost;user id=root;password=xxx;database=TvLinksMetro"" 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>
我用2个winforms项目创建了另一个简单的解决方案,第一个是我的Model1没有错误。 在这里,我引用project1并尝试从我的Model1获取相同的数据集,并再次出现相同的错误。 还快速更新我下载了所有最新的MySQl连接器和MySQl for Visual Studio,甚至将服务器更新到最新版本。在我这样做之后,我创建了这个小测试解决方案。
答案 0 :(得分:1)
问题在于定义了多个提供商:
<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>
这只会影响从二级项目调用,就像我正在做的那样。