Entity Framework 4.0 does not work with computed properties

I have an updatable view that appears in an entity structure (edmx designer)

Everything works well and well until I add a property to the updated view (and entity), labeled StoreGeneratedPattern of Computed . As soon as I do this, when saving my modified object:

 var user = objectContext.Users.FirstOrDefault(u => u.Id == 123); // user is detached and some operations are performed... // then it re-attached to a new ObjectContext and has its ObjectStateEntry set to Modified secondObjectContextInstance.SaveChanges() // throws exception: The property 'Id' is part of the object key information and cannot be modified. at System.Data.Objects.EntityEntry.VerifyEntityValueIsEditable(StateManagerTypeMetadata typeMetadata, Int32 ordinal, String memberName) at System.Data.Objects.EntityEntry.GetAndValidateChangeMemberInfo(String entityMemberName, Object complexObject, String complexObjectMemberName, StateManagerTypeMetadata& typeMetadata, String& changingMemberName, Object& changingObject) at System.Data.Objects.EntityEntry.EntityMemberChanging(String entityMemberName, Object complexObject, String complexObjectMemberName) at System.Data.Objects.EntityEntry.EntityMemberChanging(String entityMemberName) at System.Data.Objects.ObjectStateEntry.System.Data.Objects.DataClasses.IEntityChangeTracker.EntityMemberChanging(String entityMemberName) at System.Data.Objects.Internal.SnapshotChangeTrackingStrategy.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value) at System.Data.Objects.Internal.EntityWrapper`1.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value) at System.Data.Objects.EntityEntry.SetCurrentEntityValue(StateManagerTypeMetadata metadata, Int32 ordinal, Object userObject, Object newValue) at System.Data.Objects.ObjectStateEntryDbUpdatableDataRecord.SetRecordValue(Int32 ordinal, Object value) at System.Data.Mapping.Update.Internal.UpdateTranslator.SetServerGenValue(PropagatorResult context, Object value) at System.Data.Mapping.Update.Internal.UpdateTranslator.BackPropagateServerGen(List`1 generatedValues) 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() 

I have not changed the value of Id. In fact, I can reproduce this error if I don’t change anything at all and just reconnect to the second object-information object, set the modification and call Save.

I see that the SQL generated for the update reflects that I set this property as computed:

 exec sp_executesql N'update [dbo].[UsersView] set [UserName] = @0, [LastName] = @1, [FirstName] = @2, [MiddleName] = @3, [Suffix] = null, [Pid] = @4, [IsLoggedIn] = @5, [DisplayName] = @10 where ([Id] = @12) select [ComputedProperty] from [dbo].[UsersView] where @@ROWCOUNT > 0 and [Id] = @12',N'@0 nvarchar(35),@1 nvarchar(35),@2 nvarchar(35),@3 nvarchar(35),@4 nvarchar(4),@5 bit,@6 bit,@7 bit,@8 nvarchar(max) ,@9 nvarchar(max) ,@10 nvarchar(max) ,@11 int,@12 int',@0=N'yaya',...... 

Everything works fine again, without any problems, until I install ComputedProperty on the StoreGeneratedPattern of Computed. It seems like this should have something to do with the extra SELECT statement added to the SQL update message in the above ... because the exception occurs AFTER the SQL updates have already been executed.

What is wrong here?

Thanks.

+4
source share
2 answers

Setting the state state of the object of the object to be changed will cause each property to be set as changed. Instead, you should only mark properties that are explicitly modified, as shown in this answer. Entity framework 4, update specific properties

+1
source

Well, I think you could call it your own stupidity, but the behavior is not intuitive.

In case someone else implements INSTEAD OF triggers to enable updated views via EF, it should be noted that the implementation described here: an error when inserting into a table instead of a trigger from entity data framework should ONLY be used for an INSTEAD OF INSERT trigger, not trigger INSTEAD OF UPDATE. Adding SELECT to the end of the update trigger will result in the exception described in the question.

0
source

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


All Articles