1

I have a database which consists of two tables FirstTable and SecondTable". And there is a View CustomView. CustomView gets values from both tables FirstTable and SecondTable.

In my edmx, I mapped view into my model. I am getting data from the database which works fine. But if I try to update any data I am getting DbUpdateException.

Here is my code,

public UpdateData(string something)
{
    using (var database = new MyDatabase())
    {
        var records = database.Context.CustomView.ToList();
        foreach (var record in records)
        {
           database.Context.CustonView.Attach(record);
           record.Something = something;
        }
        database.Context.SaveChanges();
    }
}

Here is the exception,

{System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> iAnywhere.Data.SQLAnywhere.SAException: Syntax error near 'set' on line 16
   at iAnywhere.Data.SQLAnywhere.SACommand.ExecuteNonQuery()
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   --- End of inner exception stack trace ---
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()

If I map table FirstTable and update its values, it is working fine, but for view it fails.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • Can you execute the update in sql directly? What is the view definition and the update sql statement? (and the dbms for that matter) – John Jan 05 '15 at 11:00
  • I don't know the exact query but it gets updated (some other c++ applications update database through the view). I am connecting to a Sybase SQL anywhere database. I think problem is in Entity Framework, I have missed something, don't know, – fhnaseer Jan 05 '15 at 11:05
  • Can I get query from entity framework?, – fhnaseer Jan 05 '15 at 11:05
  • Yes you can: http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework – John Jan 05 '15 at 11:09
  • I am not using linq query. Examples show sql of selecting information. – fhnaseer Jan 05 '15 at 11:13
  • var sql = ((System.Data.Objects.ObjectQuery)database.Context.CustomView).ToTraceString(); – John Jan 05 '15 at 11:17

0 Answers0