C# Mysql EF6 Code First System.ComponentModel.Win32Exception 文件未找到

Posted

技术标签:

【中文标题】C# Mysql EF6 Code First System.ComponentModel.Win32Exception 文件未找到【英文标题】:C# Mysql EF6 Code First System.ComponentModel.Win32Exception File not Found 【发布时间】:2015-12-30 03:00:34 【问题描述】:

我只是找不到解决问题的方法。尽管每个文件都应该在那里,但我一直遇到同样的异常。它甚至没有说它缺少什么。纳达。

我的数据库在本地主机上,使用没有密码的用户 root,所以登录信息不会有问题。我在本地的其他项目上使用相同的登录信息。

我收到错误

context.Database.Create();

这是我得到的整个堆栈:

System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常, Boolean breakConnection, Action`1 wrapCloseInAction) +92
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔异步关闭)+285
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean IntegratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType) +372
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,字符串 newPassword,SecureString newSecurePassword,布尔型 ignoreSniOpenTimeout,TimeoutTimer 超时,布尔型 withFailover)+172
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +818
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer 超时,SqlConnectionString connectionOptions,SqlCredential 凭证,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance)+320
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 身份,SqlConnectionString connectionOptions,SqlCredential 凭据,对象 providerInfo,String newPassword,SecureString newSecurePassword,布尔重定向用户实例,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool 池,字符串 accessToken)+591
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +304
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)+681
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +89
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔allowCreate,布尔onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)+426
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource`1 重试,DbConnectionOptions userOptions,DbConnectionInternal& 连接)+78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource`1 重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& 连接)+191
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +154
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +90
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +166
   System.Data.SqlClient.SqlConnection.Open() +96
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) +10
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget目标,Action`2操作,TInterceptionContext拦截上下文,Action`3执行,Action`3执行)+72
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection 连接,DbInterceptionContext 拦截上下文)+360
   System.Data.Entity.SqlServer.c__DisplayClass33.b__32() +426
   System.Data.Entity.SqlServer.c__DisplayClass1.b__0() +10
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 操作)+189
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(动作操作)+78
   System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act) +175
   System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +557
   System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +86
   System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection 连接,Nullable`1 commandTimeout,StoreItemCollection storeItemCollection)+164
   System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection 连接,Nullable`1 commandTimeout,StoreItemCollection storeItemCollection)+76
   System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +134
   System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection 连接)+119
   System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase)+142
   System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78
   System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +89
   System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existsState) +116
   System.Data.Entity.Database.Create(DatabaseExistenceState 存在状态) +218
   System.Data.Entity.Database.Create() +7
   Naaku.Controllers.testController.Index() 在 d:\_Dev\Repos\Naaku\Naaku\Naaku\Controllers\testController.cs:22
   lambda_method(闭包,ControllerBase,对象[])+62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 参数) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 参数) +27
   System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50
   System.Web.Mvc.Async.c__DisplayClass46.b__3f() +225
   System.Web.Mvc.Async.c__DisplayClass33.b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.c__DisplayClass2b.b__1c() +26
   System.Web.Mvc.Async.c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9721605
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我的 web.config(如果没有解析,我似乎无法在此处获取 html...) http://pastebin.com/qLpp5VBb

        public EFDbContext()
        : base(_Configuration.mysqlConnectionString)
    
    


public static String MySqlConnectionString = "server=localhost;user id=root;Persist Security Info=True;database=naaku";

【问题讨论】:

显示 Web.config 或 App.config 更新了 OP。抱歉,pastebin.. 【参考方案1】:

您缺少默认连接工厂。这就是为什么您收到 SqlClient 错误而不是 mysql 错误的原因。 添加以下内容:到 entityFramework 节点:

<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6"></defaultConnectionFactory>

【讨论】:

以上是关于C# Mysql EF6 Code First System.ComponentModel.Win32Exception 文件未找到的主要内容,如果未能解决你的问题,请参考以下文章

亲测Asp.net Mvc5 + EF6 code first 方式连接MySQL总结

使用 EF6 Code First 的 MySQL 现有数据库 - 使用脚手架创建控制器会导致错误

如何在EF6 Code First中创建与枚举对应的表?

使用 EF6(和 Code First 迁移)针对不同的数据库系统

EF6.1.1的code first开发,出现异常: System.Data.Entity.SqlServer.SqlProviderServices, 悬赏50分

ef6 code first