The operation is invalid due to the current state of the object

I am using Entity Framework with ODP.NET 11.2.0.3.0. Perseverance worked for me for some tables, but this apartment does not allow to add a simple new object, and I can not understand it. Your help is greatly appreciated. Thanks you

Here is the code. I removed the image field from the object through the model browser, and yet the code will not work when SaveChanges () is called.

var corpDirectoryEntities = new CorpDirectoryEntities(); var cc = new EmployeePhoto(); cc.UserId = 12345; // NUMBER field cc.ImageName = "imagename"; // VARCHAR2(100) cc.Image = photoStream; // LONG RAW corpDirectoryEntities.EmployeePhotos.AddObject(cc); corpDirectoryEntities.SaveChanges(); 

The following exception is thrown.

  System.Data.UpdateException was unhandled by user code
   Message = An error occurred while updating the entries.  See the inner exception for details.
   Source = System.Data.Entity
   Stacktrace:
        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.Objects.ObjectContext.SaveChanges ()
        at Repositories.Services.CorpDirectory.CorpDirectoryRepository.SaveDirectoryAccountPhoto (Int32 accountId, Byte [] photoStream) in C: \ Dev \ Projects \ Repositories \ Services \ CorpDirectory \ CorpDirectoryRepository.cs: line 356
        at Tests.CorpDirectory.CorpDirectoryUserTestFixture.TestGetUserPhoto () in C: \ Dev \ Projects \ Repositories.Tests \ CorpDirectory \ CorpDirectoryUserTestFixture.cs: line 192
   InnerException: System.Data.EntityCommandCompilationException
        Message = An error occurred while preparing the command definition.  See the inner exception for details.
        Source = System.Data.Entity
        Stacktrace:
             at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand (DbModificationCommandTree commandTree)
             at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand (UpdateTranslator translator, Dictionary`2 identifierValues)
             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)
        InnerException: System.InvalidOperationException
             Message = Operation is not valid due to the current state of the object.
             Source = Oracle.DataAccess
             Stacktrace:
                  at Oracle.DataAccess.Client.SqlGen.DmlSqlGenerator.ExpressionTranslator.Visit (DbScanExpression expression)
                  at System.Data.Common.CommandTrees.DbScanExpression.Accept (DbExpressionVisitor visitor)
                  at Oracle.DataAccess.Client.SqlGen.DmlSqlGenerator.GenerateInsertSql (DbInsertCommandTree tree, EFOracleProviderManifest providerManifest, EFOracleVersion sqlVersion, List`1 & parameters)
                  at Oracle.DataAccess.Client.SqlGen.SqlGenerator.GenerateSql (DbCommandTree tree, EFOracleProviderManifest providerManifest, EFOracleVersion sqlVersion, List`1 & parameters, CommandType & commandType)
                  at Oracle.DataAccess.Client.EFOracleProviderServices.CreateCommand (EFOracleProviderManifest providerManifest, DbCommandTree commandTree)
                  at Oracle.DataAccess.Client.EFOracleProviderServices.CreateDbCommandDefinition (DbProviderManifest providerManifest, DbCommandTree commandTree)
                  at System.Data.Common.DbProviderServices.CreateCommandDefinition (DbCommandTree commandTree)
                  at System.Data.Common.DbProviderServices.CreateCommand (DbCommandTree commandTree)
                  at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand (DbModificationCommandTree commandTree)
             InnerException: 

+6
source share
3 answers

So, after looking at it for hours, I finally understood. I checked the .ssdl file. One of the differences between my objects was how <EntitySet ... > was defined. I changed it, and of course it works now. The EF designer is just god-awful.

 <EntitySet Name="EmployeePhoto" ... store:Name="TABLE_NAME"> <DefiningQuery> SELECT ..... </DefiningQuery> </EntitySet> 

I removed <DefiningQuery...> and replaced it with the following to fit other objects:

 <EntitySet Name="EmployeePhoto" ... /> 
+16
source

If the oracle table does not have a primary key, Entity Frameworks will force it to use a defining query when it is imported into your EF schema. Then, since this is a query, not a table, it will explode with an invalid operation exception when you try to add something to it.

+2
source

I hit this error while trying to update an old project.

Make sure your project matches the version of .net framework specified in your package file. For instance. if you specified net452, make sure your project is configured to use net 4.5.2. If your solution has multiple projects, make sure they are configured with the correct version of the .net frame.

In addition, I noticed that when you add a new data model to the project, it will get confused as to which model file to add. I believe this was due to the fact that TFS is installed in my dev block (I had an express and then the latter was installed). To solve this problem, I disconnected the project from TFS now).

0
source

Source: https://habr.com/ru/post/920248/


All Articles