0

I am getting the following error while adding new items to db table with Entity Framework 6 and MySQL as the database server.

System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
  HResult=-2146233087
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at Activity_Tracker.Models.Model.AddnewProject(Int32 id) in f:\Projects\Activity Tracker\Activity Tracker\Models\Model.cs:line 17
       at Activity_Tracker.Controllers.HomeController.Index() in f:\Projects\Activity Tracker\Activity Tracker\Controllers\HomeController.cs:line 15
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e()
  InnerException: System.Data.Entity.Core.UpdateException
       HResult=-2146233087
       Message=An error occurred while updating the entries. See the inner exception for details.
       Source=EntityFramework
       StackTrace:
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction, Boolean throwOnClosedConnection)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection)
            at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33()
            at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy)
            at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass28.<SaveChanges>b__25()
            at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
            at System.Data.Entity.Internal.InternalContext.SaveChanges()
       **InnerException: MySql.Data.MySqlClient.MySqlException
            HResult=-2147467259
            Message=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT
`tasks`.`ProjectName`
FROM `tasks` AS `tasks`)(
`ProjectName`) VALUES (' at line 1
            Source=MySql.Data
            ErrorCode=-2147467259**
            Number=1064
            StackTrace:
                 at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
                 at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
                 at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
                 at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
                 at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
                 at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
                 at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
                 at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
                 at MySql.Data.Entity.EFMySqlCommand.ExecuteNonQuery()
                 at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
                 at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
                 at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
                 at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
                 at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
            InnerException: 

The method I am calling from the model is:

public void AddnewProject(int id)
    {
       using( activity_trackerEntities en=new activity_trackerEntities())
       {
           task task = new task();
           task.ProjectName = id;
           en.tasks.Add(task);
           en.SaveChanges();// at this line i am getting exception
       }
    }

My Web.config:

<connectionStrings>    
    <add name="activity_trackerEntities" connectionString="metadata=res://*/Models.ActivityTracker.csdl|res://*/Models.ActivityTracker.ssdl|res://*/Models.ActivityTracker.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;password=mypassword;database=activity_tracker&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
<system.data>
    <DbProviderFactories>
      <add name="MySql.Data.MySqlClient" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
      type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
      <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
      <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      </providers>
  </entityFramework>
alex
  • 6,818
  • 9
  • 52
  • 103
Vikas Dagar
  • 143
  • 1
  • 1
  • 10
  • The error doesn't seem to be coming from the source code you've provided. It looks like the component that builds your SQL strings isn't doing it's job. It's trying to execute `'(SELECT tasks.ProjectName FROMtasksAStasks)( ProjectName) VALUES ('`. Notice the missing spaces in the FROM / AS – Mathew Jan 07 '14 at 17:01
  • i found this problem too and i found these links [See this][1] [and this][2] [1]: http://stackoverflow.com/questions/5449724/entity-framework-code-first-cannot-insert-duplicate-key-in-object-db [2]: http://stackoverflow.com/questions/17033895/mvc4-db-savechanges-decimal-value-out-of-range – KhogaEslam Mar 02 '14 at 13:07

0 Answers0