The EntitySpaces Community

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

Can't retrieve GUID ROWGUIDCOL Primary Key after Save()

Last post 04-03-2008, 3:01 PM by Stupid2Rules. 2 replies.
Sort Posts: Previous Next
  •  04-02-2008, 10:40 AM 8679

    Can't retrieve GUID ROWGUIDCOL Primary Key after Save()

    I read the posts about the issue with NEWSEQUENTIALID() and so changed to NEWID().  But, I'm still not getting the result I expect with an app on WM5 using SQLCE with Merge replication.

    This code generates generates a ThrowInvalidOperationException() because G.GpsId is null:

    Code:
                    AGps G = new AGps();
                    G.AddNew();
    
                    G.DeviceState = (int)Dev.DeviceState;
                    G.DriverServiceState = (int)Dev.ServiceState;
    
                    G.Save();
    
                    _lastSavedPosition_ID = G.GpsId.Value;
    

    But, this works fine:

    Code:
                    AGps G = new AGps();
                    G.AddNew();
    
                    G.GpsId = Guid.NewGuid();  // Pre-assign the GUID
    
                    G.DeviceState = (int)Dev.DeviceState;
                    G.DriverServiceState = (int)Dev.ServiceState;
    
                    G.Save();
    
                    _lastSavedPosition_ID = G.GpsId.Value;
    
    The table is:
    Code:
    CREATE TABLE [dbo].[aGps] (
    	[GpsId] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() ROWGUIDCOL,
    	 CONSTRAINT [PK_a_Gps] PRIMARY KEY NONCLUSTERED ([GpsId]) ON [PRIMARY],
    	[DeviceState] [INT] NULL,
    	[DriverServiceState] [INT] NULL,
    ) ON [PRIMARY]

    The record is saved either way (with Guids). Any ideas as to what is going on?

  •  04-02-2008, 7:04 PM 8686 in reply to 8679

    Re: Can't retrieve GUID ROWGUIDCOL Primary Key after Save()

    The reason why I believe is that SQL CE does not support output parameters. This works in our full blown SQL provider of course. However, we have yet to convert our SQL Compact Framework provider over to ES2008. That will happen very soon and we will look at enhancing some of this. The problem is how do we select the guid when it's the primary key after an insert? For now the best approach is to just continue what you're doing by assigning the Guid.

    Let me give you some tips to make things easier:

    1. You don't have to call AddNew() - EntitySpaces will do this for you automatically.
    2. You can overload AddNew() in your Entity's Custom class and assign the Guid thereby hiding it from your application code. AddNew() will always be called for you upon the first property access if it hasn't yet been called.

     

    If later we enhance the provider to pull back the Guids you can remove the AddNew() overload and you're off and running.

     


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  04-03-2008, 3:01 PM 8700 in reply to 8686

    Re: Can't retrieve GUID ROWGUIDCOL Primary Key after Save()

    Thanks Mike.  BTW, I am NOT using stored procedures.
View as RSS news feed in XML