return value for the PK of an entity

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 01-Aug-2007 17:03:01   

asp.net 2.0 llblgenpro 2 adapterTemplate

hiya,

I have a "job" entity whose PK is a GUID.

I want to create a new entity, then save it. I want to return the PK of the newly created job entity so that I can use it in the constructor of a foreign key entity, "jobItem"

How can I do this? I'd have thought that the adapter would have had a "save" method that returned this value, but I can't see it.

This is what I tried, but I now get an error stating:

Violation of PRIMARY KEY constraint 'PK_JobItem_1'. Cannot insert duplicate key in object 'dbo.JobItem'.

JobEntity job = JobFactory();
job.JobName = StateHelper.NewJobName;
job.Description = TextBox2.Text;

DataAccessAdapter adp = new DataAccessAdapter(true);

adp.SaveEntity(job);   //CAN I GET THE PK OF THE NEWLY SAVED ENTITY??

 JobItemEntity jobItem = new JobItemEntity(job.JobId);  //GRAB ID of newly created entity.
 jobItem.ProcessStageId = 1; 
etc.

 adp.SaveEntity(jobItem);   //ERROR

many thanks for any help.

cheers,

yogi

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 17:35:53   

-What's the relation between Job & JobItem? -Are they in an Inheritance Hierarchy?

JobItemEntity jobItem = new JobItemEntity(job.JobId); //GRAB ID of newly created entity.

Why are you setting the PK of JobItem with the PK of the Job?

Where/how does the GUID of the Job.JobID get created?

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 01-Aug-2007 17:56:33   

Assuming SQL Server and database default value of NEWID() or NEWSEQUENTIALID()


adp.SaveEntity(job, true);

True flag is the Refetch after Save flag.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 18:18:08   

Use the NEWSEQUENTIALID(). To use this feature in LLBLGen Pro, you've to do the following: -Specify as the default for the primary key field in the table definition: NEWSEQUENTIALID(). -Set the PK field to a new GUID value when you're saving the entity. -Set the SqlServer DQE in the .config file of your application, add the following tag to the configuration's appSettings section:

<add key="SqlServerDQECompatibilityLevel" value="2" />

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. (No need for the reeftch flag)

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 01-Aug-2007 18:47:15   

hiya,

Thanks for the replies.

ok, I've made a bit of progress.

this is the relationship

job jobId GUID (PK) jobDescription

jobItem jobItemId GUID (PK) jobId GUID (FK)

In other words, there are many jobItems to a single job. I hope that makes sense?

I'd like to post the new code so that it's clear what I should do.

JobEntity job = JobFactory();
job.Description = TextBox2.Text;
etc
DataAccessAdapter adp = new DataAccessAdapter(true);

adp.SaveEntity(job, true);

//Now, I create a new jobItem

 JobItemEntity jobItem = new JobItemEntity();
 jobItem.JobId = job.JobId;

 adp.SaveEntity(jobItem);  // Error: 

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobItem_Job". The conflict occurred in database "dbDev", table "dbo.Job", column 'JobId'.

Does this make it any clearer? I wanted to post the new stuff, just to make sure.

many thanks,

yogi

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-Aug-2007 09:48:51   

Have you set the SqlServerDQECompatibilityLevel?

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 02-Aug-2007 15:45:42   

hiya,

Thanks. Setting the SqlServerDQECompatibilityLevel worked.I still have to refetch though.At the moment I'll keep doing that because it works with the minimum of change.

many thanks Jim and Walaa.

yogi