6

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: 

user1198049
  • 491
  • 5
  • 15

3 Answers3

16

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" ... />
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
user1198049
  • 491
  • 5
  • 15
2

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.

TJ Bandrowsky
  • 842
  • 8
  • 12
0

I've been hitting this error while trying to upgrade an older project.

Make sure your project matched the .net framework version listed in your package file. E.g. if you have net452 listed, make sure your project is set to use net 4.5.2. If you have more than one project in your solution, make sure they are all set to the correct .net framework version.

Also, I noticed when adding a new data model to the project, it would get confused as to which model file to add. I believe this was due to the TFS installed on my dev box (I had express, then install latest). To solve this issue, I disconnected the project from TFS for now).

Carl Prothman
  • 1,461
  • 13
  • 23