Delete new entity without reloading grid

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
CoreyG
Posts: 2
Joined: Tue 06 Mar 2012 14:22
Location: Regina, SK, Canada

Delete new entity without reloading grid

Post by CoreyG » Mon 26 Mar 2012 17:59

I have a legacy Oracle 10g table which does not have a column defined as a Primary key but it is set to Not Null (named ID).

After I add a new entity to my DbContext, I then retrieve the Sequence generated ID:

Code: Select all

Public Shared Sub UpdateID(ByRef entity As TBLUDACMAP, context As Entities)
        Dim LastDate As Date = entity.LAST_OPERATOR_DATE

        Dim NewId As Decimal = (From tbl In context.TBLUDACMAPs
                 Where tbl.LAST_OPERATOR_DATE = LastDate
                 Select tbl.ID).Single()

        entity.ID = NewId
    End Sub
then I refresh my datagrid:

Code: Select all

BindingSource.ResetCurrentItem()
which changes the ID in grid from 0 to the proper ID value.

and finally if I try to delete this new entity which has the proper Oracle generated ID, I receive a "the property 'ID' is part of the object's key information and cannot be modified" error.

In my Entity Model ID is, of course, set to Entity Key, Nullable = False, and StoreGeneratedPattern = Identity.

Without rebinding my Bindsource, is there a way to make this work?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 28 Mar 2012 15:02

CoreyG wrote:I receive a "the property 'ID' is part of the object's key information and cannot be modified" error
It is not allowed to update properties, which are included into EntityKey, of attached entity in Entity Framework.

If this doesn't help, please send us a small test project. Include definitions of database objects and avoid using third-party components.

CoreyG
Posts: 2
Joined: Tue 06 Mar 2012 14:22
Location: Regina, SK, Canada

Post by CoreyG » Fri 30 Mar 2012 21:52

To expand on my previous code, the original call was:

Code: Select all

' If entity is new then Add.
If _IsNewEntity = IsNew.Yes Then
  _Context.TBLUDACMAPs.Add(_Entity)
End If

_Context.SaveChanges()

' Get the Oracle Sequence generated ID and update new entity.
If _IsNewEntity = IsNew.Yes Then        
  TBLUDACMAP.UpdateID(_Entity, _Context)
End If
There is where the Entity Framework didn't like me changing the Entity Key. So to fix this I removed the entity from _Context, updated the entity and then add the entity back _Context with the generated ID.

Code: Select all

' If entity is new then Add.
If _IsNewEntity = IsNew.Yes Then
  _Context.TBLUDACMAPs.Add(_Entity)
End If

_Context.SaveChanges()

' Update entity with actual ID value from the table.
' Detach entity so that the entity key can properly be updated,
' otherwise when deleting a newly created entity an error will occur.
If _IsNewEntity = IsNew.Yes Then
  _Context.Entry(_Entity).State = System.Data.EntityState.Detached

  TBLUDACMAP.UpdateID(_Entity, _Context)

  _Context.Entry(_Entity).State = System.Data.EntityState.Added
End If

Post Reply