Avoiding Referential Integrity violations

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 08-Jun-2006 13:29:01   

Hi All,

I'm still just getting into LLBLGen Pro, using Adapter, VB, VS2005 & SQL Server and I wonder what the LLBLGen approach is for the following:

In my pre LLBLGen world, when deleting a row from the parent table which formed part of a 1:n relationship, I would explicitly execute some SQL to checked if there were any FK entries in the child table, before trying to delete the parent (it was cheaper than trapping the exception).

I'm pretty sure that there is a simplier way in LLBLGen, what do you gurus do here?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Jun-2006 14:41:51   

You can perform the same select in LLBLGen Pro.

Please refer to LLBLGen Pro documentation: "Using the generated code -> SelfServicing/Adapter -> Using the entity collection classes" check the section "Entity retrieval into an entity collection object -> Using a related entity""

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 08-Jun-2006 15:24:57   

Greatttt, I'll look into it.

Thanks

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 08-Jun-2006 17:55:18   

Walaa wrote:

Please refer to LLBLGen Pro documentation: "Using the generated code -> SelfServicing/Adapter -> Using the entity collection classes" check the section "Entity retrieval into an entity collection object -> Using a related entity""

Had a look and it would appear that using the entity collection classes may be too costly, as I only need to know if there is an entry in any child table related to its parent.

The 'using a related entity' seems a good bet, but with the order being on the many side of the order/Customer relationship, in the example given, I don't know if the following is possible.

Division has one or more Sections. A Section is part of one Division only. Division has one or more Employees. An Employee belongs to one Division only.

Dim Division As New DivisionEntity(10)
Dim adapter As New DataAccessAdapter(True)
adapter.FetchEntity(Division)
Division.Section = CType(adapter.FetchNewEntity(New SectionEntityFactory(), _
    Division.GetRelationInfoSection()), SectionEntity)
Division.Employee = CType(adapter.FetchNewEntity(New EmployeeEntityFactory(), _
    Division.GetRelationInfoEmployee()), EmployeeEntity)
adapter.CloseConnection()

Given that Division is on the 1 side of the relationship, can you see any problem with this? Would I just get the first Section or Employee whose FK matched the Division?

But since LLBLGen knows about the relationships, is there any functionality within LLBLGen to retrieve related information if its just given the parent details. That would mean not having to explicitly specify each child table. smile

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 09-Jun-2006 10:24:14   

Markiemac wrote:

Walaa wrote:

Please refer to LLBLGen Pro documentation: "Using the generated code -> SelfServicing/Adapter -> Using the entity collection classes" check the section "Entity retrieval into an entity collection object -> Using a related entity""

Had a look and it would appear that using the entity collection classes may be too costly, as I only need to know if there is an entry in any child table related to its parent.

You can always use a Scalar query to fetch a PK value and if it's null, it's not there, which answers the question if the related entity is there or not simple_smile

The 'using a related entity' seems a good bet, but with the order being on the many side of the order/Customer relationship, in the example given, I don't know if the following is possible.

Division has one or more Sections. A Section is part of one Division only. Division has one or more Employees. An Employee belongs to one Division only.

Dim Division As New DivisionEntity(10)
Dim adapter As New DataAccessAdapter(True)
adapter.FetchEntity(Division)
Division.Section = CType(adapter.FetchNewEntity(New SectionEntityFactory(), _
    Division.GetRelationInfoSection()), SectionEntity)
Division.Employee = CType(adapter.FetchNewEntity(New EmployeeEntityFactory(), _
    Division.GetRelationInfoEmployee()), EmployeeEntity)
adapter.CloseConnection()

Given that Division is on the 1 side of the relationship, can you see any problem with this? Would I just get the first Section or Employee whose FK matched the Division?

I wouldn't do it this way. You already know the PK, '10', so you can use that in a filter to execute a direct delete on Section and Employee by using adapter.DeleteEntitiesDirectly(). This will execute a DELETE FROM .. WHERE ... and the WHERE is thus of course your specified filter, thus filtering on DivisionID = 10 (or whatever fk field in section and employee points to division)

But since LLBLGen knows about the relationships, is there any functionality within LLBLGen to retrieve related information if its just given the parent details. That would mean not having to explicitly specify each child table. smile

Thanks

Yes you have. The reason is that LLBLGen Pro doesn't 'assume' something for you, you've to tell it what to do (to a certain point of course wink ). This gives the biggest flexibility and also the biggest feature set. The thing with relations is that they're hard to specify, as they don't have a unique number or something, they have a start and end entity, and also PK and FK fields. That can be a whole set of values, and how to make a unique key out of that? that's pretty complex, if not impossible. So LLBLGen Pro offers you the relations in code, like CustomerEntity.Relations.OrderEntityUsing... but you can't get a list of relations and then traverse them, because how will you know you found the relation you want?

I understand that it in your case means that you have to know all entities which have an FK to division / relation with division. But such information is inevitable anyway for writing a good db consuming application.

Frans Bouma | Lead developer LLBLGen Pro