Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation is not valid due to the current state of the object

I'm using Entity Framework with ODP.NET 11.2.0.3.0. I got persistence working for some tables, but this one flat out refuses to add a simple new object and I can't quite figure it out. Your help is greatly appreciated. Thank you

Here is the code. I removed the image field from the object through the model browser and still the code fails 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 occurs.

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: 

like image 500
user1198049 Avatar asked Jul 11 '12 17:07

user1198049


2 Answers

So, after looking at it for hours, I finally got it. I checked the .ssdl file. One difference between my objects was in how the <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 the <DefiningQuery...> and replaced it with the following to match other entities:

<EntitySet Name="EmployeePhoto" ... />
like image 131
user1198049 Avatar answered Sep 18 '22 22:09

user1198049


If there's no primary key on an oracle table, Entity Frameworks will make it use a defining query when it is imported into your EF schema. Then, since it is a query, not a table, it will blow up with an invalid operation exception when you try to add something to it.

like image 39
TJ Bandrowsky Avatar answered Sep 20 '22 22:09

TJ Bandrowsky