使用实体框架和MySQL

时间:2017-05-19 18:49:55

标签: c# mysql entity-framework masstransit automatonymous

我正在使用MassTransit(使用RabbitMQ)和Automatonymous开发一个传奇。我使用EntityFramework作为我的商店引擎,MySQL作为我的数据库。每当我尝试运行saga并将其状态存储在数据库中时,EF和MySQL的组合就会抛出异常。使用this示例应用程序并使用连接字符串到MySQL数据库可以重现该问题。引发的确切异常是:

  

System.Data.SqlClient.SqlException(0x80131904):与网络相关或   建立连接时发生特定于实例的错误   SQL Server。服务器未找到或无法访问。 Ve rify   实例名称正确且SQL Server配置为   允许远程连接。 (提供者:命名管道提供商,错误:40 -   无法打开与SQL Server的连接)---&gt; SYSTE   m.ComponentModel.Win32Exception(0x80004005):系统找不到   指定的文件   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity   identity,SqlConnectionString connectionOptions,SqlCredential   凭证,Object providerInfo,String newPassword,SecureString   newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString   userConnectionOptions,SessionData reconnectSessionData,   DbConnectionPool池,String accessToken,Boolean applyTra   nsientFaultHandling)at   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions   options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,   DbConnectionPool池,DbConnection拥有连接,   DbConnectionOptions userOptions)at   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(类DBConnectionPool   pool,DbConnection owningObject,DbConnectionOptions选项,   DbConnectionPoolKey poolKey,DbConnectionOption的userOptions)at   System.Data.ProviderBase.DbConnectionPool.CreateObject(的DbConnection   owningObject,DbConnectionOptions userOptions,DbConnectionInternal   oldConnection)   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(的DbConnection   owningObject,DbConnectionOptions userOptions,DbConnectionInternal   oldConnection)   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection   owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean   allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptio ns   userOptions,DbConnectionInternal&amp;连接)   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection   owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions   userOptions,DbConnectionInternal oldConnection,Db   ConnectionInternal&安培;连接)   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(的DbConnection   outerConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource 1 retry, DbConnectionOptions use rOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试,DbConnectionOptions userOptions)at   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1   重试)在System.Data.SqlClient.SqlConnection.Open()处   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(的DbConnection   t,DbConnectionInterceptionContext c)at   System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action 2操作,TInterceptionContext interceptionContext,   行动3 executing, Action 3已执行)   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(的DbConnection   connection,DbInterceptionContext interceptionContext)at   System.Data.Entity.SqlServer.SqlProviderServices&LT;&GT; c__DisplayClass33.b__32()   在   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy&LT;&GT; c__DisplayClass1.b__0()   在   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func 1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action 1 act)at at   System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(的DbConnection   sqlConnection,Action 1 act) at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable 1   commandTimeout,DbConnection sqlConnection,String   createDatabaseScript)at   System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(的DbConnection   connection,Nullable 1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable 1 commandTimeout,StoreItemCollection   storeItemCollection)at   System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()at   System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(的DbConnection   连接)   System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作   mustSucceedToKeepDatabase)at   System.Data.Entity.Migrations.DbMigrator.Update(字符串   targetMigration)   System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext   internalContext,Func 3 createMigrator, ObjectContext objectContext) at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) at System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) at System.Data.Entity.CreateDatabaseIfNotExists 1.InitializeDatabase(TContext   上下文)   System.Data.Entity.Internal.InternalContext&LT;&GT; c__DisplayClassf 1.<CreateInitializationAction>b__e() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction 1.PerformAction(TInput   输入)   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(动作1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext() at System.Data.Entity.Database.BeginTransaction(IsolationLevel isolationLevel) at MassTransit.EntityFrameworkIntegration.Saga.EntityFrameworkSagaRepository {1.d__8 {1}} 2.&GT; - 发送&GT; d__6.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   MassTransit.Saga.Pipeline.Filters.QuerySagaFilter 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at MassTransit.Saga.Pipeline.Filters.QuerySagaFilter 1.d__3.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()at   MassTransit.Pipeline.Filters.TeeConsumeFilter 2.<MassTransit-Pipeline-IFilter<MassTransit-ConsumeContext<TMessage>>-Send>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at MassTransit.Saga.Pipeline.Filters.CorrelationIdMessageFilter 1.d__7.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()at   MassTransit.Pipeline.Filters.MessageConsumeFilter 1.<>c__DisplayClass7_0.<<Send>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at MassTransit.Pipeline.Filters.TeeConsumeFilter 1-SEND&GT; d__7.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()at   MassTransit.Pipeline.Filters.DeserializeFilter.d__4.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()at   MassTransit.Pipeline.Filters.RescueReceiveContextFilter`1.-SEND&GT; d__5.MoveNext()   ClientConnectionId:00000000-0000-0000-0000-000000000000错误   号:2,状态:0,类别:20

如果我使用VS 2015安装的本地数据库而不是MySQL,那么它的工作原理非常好。它也适用于内存存储。即使异常说无法找到服务器,我也可以确认它是否存在且可以访问,因为示例应用程序在选择连接字符串时首先进行检查并且成功:

1.<MassTransit-Pipeline-IFilter<MassTransit-ConsumeContext>-Send>d__7.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---  at
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  at
  MassTransit.Pipeline.Filters.MessageConsumeFilter

问题可能是因为它试图用SqlClient访问MySQL数据库吗?

我使用的是MySQL Server 5.7,EF 6.1.3,MassTransit 3.5.7和Automatonymous 3.5.11。

1 个答案:

答案 0 :(得分:0)

这不是MassTransit问题,而是实体框架问题。应该是这样的:

如何在MySQL中使用Entity Framework。

你的例外清楚地说:System.Data.SqlClient正在抛弃它。这是SQL Server客户端。它还说:

  

无法打开与SQL Server的连接

您可以参考documentation了解如何配置EF和MySQL。通常,您需要安装MySql.Data.Entity NuGet包并告诉EF您正在使用MySQL,然后配置正确的连接字符串:

<connectionStrings>
    <add name="MyContext" providerName="MySql.Data.MySqlClient"
        connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********"/>
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <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>
相关问题