Odd GUID behavior?

Posts   
 
    
ChrisMoses
User
Posts: 18
Joined: 22-Feb-2005
# Posted on: 16-Nov-2005 07:24:39   

Hi, I am having an unusual issue that I have never run into before.

General: VS.Net 2003 Self Servicing Ms Access

I have an object, CategoryEntity, which uses GUIDs (ReplicationID in Access) as a primary key. After I create an object and save it, it still has a value of 00000000-0000-0000-0000-000000000000, even after I call refetch. It has been added to the DB sucessfully, and assigned a propery GUID by the db.

The code is simply: Dim oCat As New CategoryEntity oCat.CategoryName = "category " & oLoop.ToString ... set properties ... oCat.Save() oCat.Refetch() Console.WriteLine("Added Top Level cat: " & oCat.CategoryId.ToString)

CategoryID is the primary key and is set to autonumber and ReplicationID.

Should I be calling something else to refresh it after the save? Should I really have to refresh it after the save?

Thanks, WCM

ChrisMoses
User
Posts: 18
Joined: 22-Feb-2005
# Posted on: 16-Nov-2005 07:51:05   

Note- Everything appears to work fine if I manually set the GUID: Category.CategoryID = guid.NewGUID.

This isn't a big problem, but I know somebody is going to forget to do it at some point. If there is anyway to avoid having to do this it would be nice.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Nov-2005 08:38:57   

Guids aren't read back from the database because teh value a NEWID() call will return isn't readable like an identity field is, so the generated PK will never be read back, which means you have to fill in the PK field manually, using Guid.NewGuid()

Frans Bouma | Lead developer LLBLGen Pro
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 16-Nov-2005 14:52:42   

ChrisMoses wrote:

Note- Everything appears to work fine if I manually set the GUID: Category.CategoryID = guid.NewGUID.

This isn't a big problem, but I know somebody is going to forget to do it at some point. If there is anyway to avoid having to do this it would be nice.

Be warned I am a new user of LLBLGen! This is the approach that I am attempting to use.

Seems to be working fine so far, but app is still in the development stages and not in production yet.

I create a custom IEntityValidatorand IValidator for each entity



In the business layer factory, I create my Vendor objects with:

_Vendor.EntityValidatorToUse = new LTInfo.ORM.VendorEntityValidator();
_Vendor.Validator = new LTInfo.ORM.VendorValidator();


    public partial class VendorValidator : IValidator
    {

        public bool Validate(int fieldIndex, object value)
        {
            bool validationResult = true;
            if ((((LTInfo.ORM.VendorFieldIndex)(fieldIndex))) == VendorFieldIndex.VendorId)
            {
                validationResult = !((((Guid)(value)).Equals(Guid.Empty)));
            }
            else if ((((LTInfo.ORM.VendorFieldIndex)(fieldIndex))) == VendorFieldIndex.VendorName)
            {
                validationResult = (value.ToString().Length > 0);
            }
            else
            {
                return true;
            }
            return validationResult;
        }
    }

public class VendorEntityValidator : IEntityValidator
    {

        public bool Validate(object containingEntity)
        {
            LTInfo.ORM.EntityClasses.VendorEntity vendor = ((LTInfo.ORM.EntityClasses.VendorEntity)(containingEntity));
            if (vendor.IsNew)
            {
                vendor.VendorId = System.Guid.NewGuid();
            }
            return true;
        }
    }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2005 09:29:27   

THanks Jim! smile

Frans Bouma | Lead developer LLBLGen Pro
ChrisMoses
User
Posts: 18
Joined: 22-Feb-2005
# Posted on: 19-Nov-2005 06:20:22   

Thanks everyone, I'll play with it.