SelfService and Save() on new Entities: Default Values

Posts   
 
    
rstrahl
User
Posts: 17
Joined: 24-Aug-2009
# Posted on: 18-Sep-2009 02:44:36   

Hi,

Ran into a small issue that I can't quite figure out while creating new entities that have default values set on insert. This is with SelfService.

For testing I created a very simple table that contains a Guid that is a PK and name field. The Guid has default value of (newid()) so it should generate a new GUID when inserted:

        
            TestTableEntity table = new TestTableEntity();

            // table.id = Guid.New();
            table.Name = "rick";
            table.Save();

            Console.WriteLine(table.Id);  // { 00...}

The save works and I get a new record in the DB but along with a new Guid, but after the Save() the entity's Id property is not updated. It remains at Guid.Empty(). An explicit Refetch also doesn't help in this case.

Worse a subsequent change and save will fail because the entity is marked as Fetched but in effect has no PK.

According to the documentation it sounds like a Save() followed by any property access should do an automatic refetch of the data and that is indeed happening but it's using the Guid.Empty() on the subsequent DB call.

I realize there's an easy fix for this here by just pre-assigning the Guid before saving but I'm trying to understand why this doesn't work here, while it does work when I use an AutoInc integer field (which is why I didn't notice this until now :-}).

What determines whether the PK is being retrieved after an update? Auto-inc on the field?

+++ Rick ---

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Sep-2009 10:33:55   

Please use NEWSEQUENTIALID() instead.

This has been mentioned in the docs.

Using the entity classes:

Fields which get their values from a trigger, from newid() or a default constraint calling a user defined function are not considered sequenced fields and these values will not be read back, so you'll have to supply a value for these fields prior to saving the entity. This isn't true for fields which are of type unique_identifier on SqlServer 2005 when the DQE is set in SqlServer 2005 compatibility mode and the field has in the database a default value of NEWSEQUENTIALID().

Database specific features:

When you're using unique_identifier types for primary keys on SqlServer 2005, you can benefit from the new feature of SqlServer 2005 called NEWSEQUENTIALID(). This feature allows you to auto-generate new GUIDs for your primary keys which are sequential, so they are friendly for clustered indexes. To use this feature in LLBLGen Pro, you've to specify as the default for the primary key field in the table definition: NEWSEQUENTIALID(). Furthermore, you shouldn't set the PK field to a new GUID value when you're saving the entity. Before you save the entity set the SqlServer Dynamic Query Engine (DQE) in the SqlServer 2005 compatibility mode (see below). The DQE will then figure out to let the database insert the NEWSEQUENTIALID() produced value and it's automatically retrieved for you into the entity's PK