Field Mapping

Posts   
 
    
dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 06-Apr-2006 23:08:46   

stupid newbie question... If I am in the designer and I am mapping an Employee entity that has a column in the db named Department of type uniqueidentifier (SQL 2000) and I want the field on my generated object to return a Department object rather than a System.GUID, how do I do that? Or do I have to extend the Employee Object and write code in the concrete class?

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 07-Apr-2006 00:25:34   

Hey, there. LLBLGen automatically generates entity properties for you based on the relationships created between tables. So, if you look at the generated Employee entity in code, you'll find Employee.Department there automatically. However, you'll probably want to rename the Department column to DepartmentID or something similar. simple_smile

Jeff...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Apr-2006 09:30:15   

Jeff is correct

Just make sure the relation between the Employee & Departement exists in the Database and in the LLBLGen Pro Designer.

Then in your code if you want to fetch an Employee with its corresponding Departement object, you should use PrefetchPathing

For that please refer to the "Using the generated code -> Adapter/SelfServicing -> Prefetch paths" in the LLBLGen Pro documentation manual

dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 07-Apr-2006 19:55:13   

ok, so... sorry if I am essentially asking the same question again

I have 2 tables...

Employee

UID uniqueidentifier(PK) LastName varchar(100) FirstName varchar(100) DepartmentID uniqueidentifier (FK)

Department

UID uniqueidentifier(PK) Name varchar(100)

There is a relationship from Employee to Department on Emplyee.DepartmentUID = Department.UID

When I add the entities, a field named Employee gets added to the Department Entity based on the relationship. Likewise on the Employee object, a field named Department has been add based on the relationship. I would expect the second action, as an Employee "has a" Department. But a Department doesn't have an Employee. (It has, or can have Employees). So, do I remove the related field Employee from the Department Entity and then add a Typed List (of Employee Entities)?

I hope this makes sense.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 08-Apr-2006 12:09:51   

dazedorconfused wrote:

ok, so... sorry if I am essentially asking the same question again

I have 2 tables...

Employee

UID uniqueidentifier(PK) LastName varchar(100) FirstName varchar(100) DepartmentID uniqueidentifier (FK)

Department

UID uniqueidentifier(PK) Name varchar(100)

There is a relationship from Employee to Department on Emplyee.DepartmentUID = Department.UID

When I add the entities, a field named Employee gets added to the Department Entity based on the relationship. Likewise on the Employee object, a field named Department has been add based on the relationship. I would expect the second action, as an Employee "has a" Department. But a Department doesn't have an Employee. (It has, or can have Employees). So, do I remove the related field Employee from the Department Entity and then add a Typed List (of Employee Entities)? I hope this makes sense.

An FK is a referential integrity relation which is interpreted as 2 relations: PK -> FK and FK -> PK.

You shouldn't look at it like 'This department can have 0 employees, so it shouldn't have an employees collection', as that would cause problems in your code because what happens when a department does have employees?

You should look at it as: Department can have 0 or more employees, and therefore has a relation Department 1:n Employee, and a field mapped onto that relation, namely Employees (you can rename these fields in the designer).

This then is generated into code and a Department instance (object) has an Employees collection but it can be empty, which means it doesn't have any employees, but it cal also be filled in the case where it does have employees.

The relation IS there, due to the Employee -> Department relation.

In the current version you can hide relations though that's not recommended for 1:n relations. We'll remove that feature in v2.

Frans Bouma | Lead developer LLBLGen Pro
dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 10-Apr-2006 17:58:23   

THanks Otis, You just answered the question that I posted in the Generated Code section as well. Sorry you had to hammer this into my skull. I think I get it now.

Employee.Department returns a Department Entity Department.Employee Returns a Collection of Employee Entities (which could be empty)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 10-Apr-2006 18:33:23   

Glad it's solved simple_smile

Frans Bouma | Lead developer LLBLGen Pro
dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 10-Apr-2006 18:50:06   

So, I went back and unhid the relationship in the Department Entity, regenned the code and recompiled.

My code looks like... Dim d As New DepartmentEntity(Guid.NewGuid) d.Name = "Information Technology"

    Dim e As New EmployeeEntity(Guid.NewGuid)
    e.LastName = "Hill"
    e.FirstName = "Steven"
    e.Department = d
    e.Save(True)

But I still get an exception when running...

An unhandled exception of type 'SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException' occurred in com.cooperlighting.objects.test.dll

Additional information: An exception was caught during the execution of an action query: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_tblEmployee_tblDepartment'. The conflict occurred in database 'test', table 'tblDepartment', column 'UID'. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 10-Apr-2006 19:24:08   

Turn it around.

d.Save(True) ' (will also save the Employee collection)

An employee can't exist until the department exists in the database with the current relationship constraint in place.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2006 08:49:07   

What's column UID in tblDepartement doing?

Do you have another relation defined between Departement and Employee, maybe the departement manager employee or so ?!!

dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 11-Apr-2006 16:05:24   

Employee is a Person Employee has a Department Employee has a Company Department has a Manager Manager is a Employee

Sorry if I am waisting your time.

Is there a way I could send you the ERD?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2006 16:08:45   

So that was the reason for the exception.

You inserts a Departement without specifying its manager, the UID doesn't allow null in the database, and you don't assign a value to it.

dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 11-Apr-2006 16:18:29   

flushed I am an idiot. I was focusing so much on the relationship between the Employee and the Department. Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2006 16:23:42   

Take it easy, these things always happen.

It just easier for somebody else to spot them than those who are deeply involved.

dazedorconfused avatar
Posts: 89
Joined: 06-Apr-2006
# Posted on: 11-Apr-2006 16:51:09   

Another, somewhat related question... if I may...

We have a showroom called the Source... when people tour it, its called a Walkthrough

a Walkthrough has SourceWalkThroughVisitor(s) SourceWalkthroughVisitor has a CompanyUID and a PersonUID

So when I add my Company Entity, on the Fields on relations tab, I get a field that Returns a typed collection with one or more instances of the entity 'SourceWalkthrough' which are indirectly related to the instance of the entity 'Company' via a relation with the intermediate entity 'SourceWalkthroughVisitor'.

Can I safely hide this relationship, as I never have a need to go from the company to the SourceWalkthrough?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 11-Apr-2006 17:30:24   

You can safely hide m:n relationships you don't need. simple_smile You can do that in bulk using the plugin, or set the preference to hide m:n relations by default and only un-hide the ones you need. Often m:n relations are found but not really needed.

Frans Bouma | Lead developer LLBLGen Pro