PK as GUID

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 10-Nov-2005 20:57:33   

Okay, this is the first project that I have done that has a GUID as a primary key and it is giving me fits cry (perhaps I have set up the database incorrectly? flushed ).

First off, after inserting a new entity the PK field (again, a GUID) is not returned to the object. Does not support @@Identity? Oh well, so I will generate the GUID in code.

So I do something like this:

MyEnity entity = new MyEntity(); entity.Name = "Me"; entity.Save(); Debug.WriteLine(entity.PK.ToString()) //prints 00000000-0000-0000-0000-000000000000

this also happens if I do this: //replace MyEnity entity = new MyEntity(); with MyEnity entity = new MyEntity(Guid.NewGuid());

Now the GUID in the database doesn't match the generated one in code? I then remove the (newid()) default in SQL and get the error: "Cannot insert the value NULL into column 'PK', table "

Problem 2:

Say I go to the database and look at the new entity that was created with Name "Me" and do this:

MyEnity entity = new MyEntity(//GUID of Me); Debug.Assert(entity.Name == "Me") //passes which means the entity was correctly fetched; sunglasses entity.Name = "NewName"; entity.Save()//throws error: frowning

"The entity 'MyEntity' doesn't have a PK defined. The update query will therefore affect all entities in the table(s), not just this entity. Please define a Primary Key field in the designer for this entity."

However in the designer and database the PK is set (to a GUID).

Any ideas?

Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 10-Nov-2005 22:37:42   

Strange here is what I found out. MyEnity entity = new MyEntity(Guid.NewGuid()); entity.Save();

doesn't work: even though entity.PK.ToString() shows a new guid...(get the no null inserts error).

BUT:

MyEnity entity = new MyEntity(); entity.PK = Guid.NewGuid(); entity.Save();

Works like a charm sunglasses

However problem 2 still exists:

MyEnity entity = new MyEntity(//GUID of Me); Debug.Assert(entity.Name == "Me") //passes which means the entity was correctly fetched;
entity.Name = "NewName"; entity.Save()//throws error:

"The entity 'MyEntity' doesn't have a PK defined. The update query will therefore affect all entities in the table(s), not just this entity. Please define a Primary Key field in the designer for this entity."

However in the designer and database the PK is set (to a GUID).

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 11-Nov-2005 10:25:35   

Sam wrote:

Strange here is what I found out. MyEnity entity = new MyEntity(Guid.NewGuid()); entity.Save();

doesn't work: even though entity.PK.ToString() shows a new guid...(get the no null inserts error).

BUT:

MyEnity entity = new MyEntity(); entity.PK = Guid.NewGuid(); entity.Save();

Works like a charm sunglasses

However problem 2 still exists:

MyEnity entity = new MyEntity(//GUID of Me); Debug.Assert(entity.Name == "Me") //passes which means the entity was correctly fetched;
entity.Name = "NewName"; entity.Save()//throws error:

"The entity 'MyEntity' doesn't have a PK defined. The update query will therefore affect all entities in the table(s), not just this entity. Please define a Primary Key field in the designer for this entity."

However in the designer and database the PK is set (to a GUID).

Wow, it's been a long time since I've used Self-Servicing. simple_smile Basically, as I remember it, passing the Guid into the constructor designates the entity as an existing entity (.IsNew == false). So, specifying a new Guid there means that LLBLGen is going to do a search in the DB for the record matching that ID. That's why using the empty constructor works; you're creating a "new" entity (.IsNew == true), setting its values, then saving it. LLBLGen uses the .IsNew property to determine whether to perform an INSERT or an UPDATE.

The second error doesn't makes sense as it seems the constructor accepts your Guid in as a PK just fine.

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Nov-2005 11:32:17   

Please continue here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=4714

It's about the same topic.

Frans Bouma | Lead developer LLBLGen Pro