Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> return value for the PK of an entity
 

Pages: 1
LLBLGen Pro Runtime Framework
return value for the PK of an entity
Page:1/1 

  Print all messages in this thread  
Poster Message
yogiberr
User



Location:

Joined on:
29-Jun-2005 13:26:35
Posted:
432 posts
# 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:

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


Code:
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

  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14626 posts
# Posted on: 01-Aug-2007 17:35:53.  
-What's the relation between Job & JobItem?
-Are they in an Inheritance Hierarchy?

Quote:
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?


  Top
JimHugh
User



Location:
Davis, CA
Joined on:
16-Nov-2005 14:32:25
Posted:
191 posts
# Posted on: 01-Aug-2007 17:56:33.  
Assuming SQL Server and database default value of NEWID() or NEWSEQUENTIALID()

Code:

adp.SaveEntity(job, true);


True flag is the Refetch after Save flag.
Jim Hughes  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14626 posts
# 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:
Code:
<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)


  Top
yogiberr
User



Location:

Joined on:
29-Jun-2005 13:26:35
Posted:
432 posts
# 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.


Code:
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:


Quote:
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
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14626 posts
# Posted on: 02-Aug-2007 09:48:51.  
Have you set the SqlServerDQECompatibilityLevel?

  Top
yogiberr
User



Location:

Joined on:
29-Jun-2005 13:26:35
Posted:
432 posts
# 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
  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.