Okay, this is the first project that I have done that has a GUID as a primary key and it is giving me fits
(perhaps I have set up the database incorrectly?
).
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;
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).
Any ideas?