The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Value of the new GUID primary key is not retrieved after entity.save()

Last post 09-04-2008, 4:40 AM by Mike.Griffin. 7 replies.
Sort Posts: Previous Next
  •  08-10-2008, 1:48 PM 10659

    Sad [:(] Value of the new GUID primary key is not retrieved after entity.save()

    Hello,

    I'm currently facing a problem. I've just modified my database and replaced most identity pk's with UNIQUE IDENTIFIERS (guid), The default column value is NewID(). I did this in order to make my DB more replication and synchronization friendly(New requirement). So now I have to massively update my source code. But I get a lot of unexpected behavior because my entities don't retrieve the NewID after being saved. I searched the forum and saw that some folks had reported this in april 2008 (about SQL SERVER CE i think).  For your information, I  generated my "generated" classes after having modified  the database and I am not using SP.

    Would it be possible fix this in the next maintenance release? I know it's due sometime early next week, but it'd be life saving. The work around would be to generate the guid myself but this would the last option.

    In case your tests don't show anything weird, could I have some advice on how to fix this?  I mean a fix, not a work around that'll take an enormous amount of time to implement (no offense).

    --------------------------------------------------------------------
    Quick edit:  EntitySpaces 0623  VistaDB build 57
    --------------------------------------------------------------------
     

     


    Thanks!

    - El pipo -
  •  08-10-2008, 7:17 PM 10661 in reply to 10659

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    Even though VistaDB does support the NewID() method what they really use is GUID() try that on one of your tables. If that doesn't work we will get you a hotfix, the release has already been cut. It might take a few days however.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  08-11-2008, 5:44 AM 10665 in reply to 10661

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    Thank you for your time. Unfortunately setting the default value to GUID() instead of NewID() didn't help (I regenerated the classes after applying the new default value). However inheriting from a custom base class in which the Save() method is overridden seems to do the trick just fine and didn't take much time to implement as you can see Wink :

    Code:
        Public Overrides Sub Save()

            'Generate guid for new rows only
            If Me.Row.RowState = DataRowState.Added Then
                'I am not looking for composite keys
                If Me.Meta.Columns.PrimaryKeys.Count = 1 Then
                    'Only if the PK has a datatype of Guid
                    If Me.Meta.Columns.PrimaryKeys.Item(0).Type Is GetType(Guid) Then
                        Me.SetColumn(Me.Meta.Columns.PrimaryKeys.Item(0).Name, Guid.NewGuid)
                    End If
                End If
            End If

            MyBase.Save()

        End Sub



    Thanks!

    - El pipo -
  •  08-11-2008, 6:43 AM 10671 in reply to 10665

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    Ahh, another way would be to overload AddNew() in your Custom class (of the single Entity) and just set the correct property to Guid.NewGuid, however, I realize your way is more generic. We will make sure we bring the values back from Save on a future maintenance release and then you will be able to remove that code.
    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  09-02-2008, 12:17 PM 11096 in reply to 10671

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    In fact, I overloaded the entity.addnew() method.

    Code:
        Public Overrides Sub AddNew()
    MyBase.AddNew()
    'I am not looking for composite keys If Me.Meta.Columns.PrimaryKeys.Count = 1 Then 'Only if the PK has a datatype of Guid If Me.Meta.Columns.PrimaryKeys.Item(0).Type Is GetType(Guid) Then
    Me
    .SetColumn(Me.Meta.Columns.PrimaryKeys.Item(0).Name, Guid.NewGuid)
    End If
    End If

    End Sub

    But since that, I've been having some weird behavior, like inserts that work well but update that don't commit ( exceptions saying that 0 rows were affected instead of 1) or that throw concurrency errors, especially when wrapped in a "using scope as new esTransactionScope" clause and on collection.save(). When replace my saves by sql update queries, everything is runs just fine.

    Do you think the overloaded AddNew() method could be the cause of all this? As I said in a previous post, everything was running fine before I changed my identity PK's to UniqueIdentifier's.

    It's a bit hard to provide a sample project that reproduces these errors... would it be possible to have entityspaces retrieve the new guid primary key on inserts on the next maintenance release ?
     

     

     


    Thanks!

    - El pipo -
  •  09-02-2008, 3:50 PM 11100 in reply to 11096

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    The one thing about calling SetColumn() is that it doesn't mark the column as dirty.

    Try adding this to your code after you call Me.SetColumn()

    Me.MarkFieldAsModified(Me.Meta.Columns.PrimaryKeys.Item(0).Name))
    However, once you call Save this shouldn't effect your updates, but give that a try.

     



    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  09-04-2008, 1:50 AM 11156 in reply to 11100

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    Thanks, I've added this line of code but it doesn't seem to change much.

    I think the best solution would be not to overload AddNew() (therefore not interfering with RowState and stuff) ... but that is only possible if EntitySpaces retrieves the NewID() for me  Angel
     


    Thanks!

    - El pipo -
  •  09-04-2008, 4:40 AM 11157 in reply to 11156

    Re: Value of the new GUID primary key is not retrieved after entity.save()

    I cannot imagine that this is causing a problem. Can you please post your CREATE TABLE statement from your database along with code that will fail. Otherwise we have nothing to go on or reproduce to find out if there is a problem here.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
View as RSS news feed in XML