newsequentialid() and refetch again

Posts   
 
    
Kjelli
User
Posts: 27
Joined: 11-May-2006
# Posted on: 24-May-2006 16:39:18   

Hi

From your post jan 20 2006 (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=4969&HighLight=1) you say it works like a charm.

What's the status now? Will I ever be able to call .Save() and have my sequential guid / guid without creating new guid from code?

It's the performance issue I have in mind.

Kjelli

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 24-May-2006 16:57:46   

It's available in v2 and thus in this beta code simple_smile .

You've to set the SQLServer DQE in 2005 mode, by using the DynamicQueryEngine.CompatibilityLevel property, or by using the config file setting. Typically you'll use: DbUtils.SetSqlServerCompatibilityLevel( SqlServerCompatibilityLevel.SqlServer2005 );

somewhere at the start of your application. You can also use the config file and do: <add key="SqlServerDQECompatibilityLevel" value="2"/>

in the appSettings section.

Then, the table has to have a DEFAULT constraint for the PK, and that default constraint has to be NEWSEQUENTIALID(); or better: (newsequentialid()) when you specify it in that horrible tool called SQL Server management studio.

The PK field obviously has to be a uniqueidentifier column.

like: CREATE TABLE [dbo].[NewSequentialIDTest]( [ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_NewSequentialIDTest_ID] DEFAULT (newsequentialid()), [Description] varchar COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, CONSTRAINT [PK_NewSequentialIDTest] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]

then you can do in code:


/// <summary>
/// Inserts a new entity in a table which has a PK with as default value newsequentialid(). This will generate a GUID inside the db and return it to the 
/// entity.
/// </summary>
[Test]
public void NewSequentialIdInsertReadBackTest()
{
    // switch to 2005 mode
    DbUtils.SetSqlServerCompatibilityLevel( SqlServerCompatibilityLevel.SqlServer2005 );

    NewSequentialIdtestEntity newIdEntity = new NewSequentialIdtestEntity();
    newIdEntity.Description = "UnitTestInsert";
    Assert.IsTrue( newIdEntity.Save());
    Guid id = newIdEntity.Id;
    Console.WriteLine( "Id : {0}", newIdEntity.Id.ToString() );
}

Frans Bouma | Lead developer LLBLGen Pro
Kjelli
User
Posts: 27
Joined: 11-May-2006
# Posted on: 24-May-2006 17:08:36   

Thank you!

Happy ascension week end!! I'm off!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 24-May-2006 19:07:57   

Kjelli wrote:

Thank you!

Happy ascension week end!! I'm off!

smile you too! simple_smile

Frans Bouma | Lead developer LLBLGen Pro