Entity inheritance: How to implement foreign key in descendants?

Posts   
 
    
horo
User
Posts: 41
Joined: 01-Sep-2005
# Posted on: 04-Aug-2008 12:18:28   

Hi to All

EA entity has two descendants: EAX and EAY EAX has an EB (EAX has a foreign key which references to EB) EAY has an EC (EAY has a foreign key which references to EC)

Unfortunatelly I dont know how to implement this with LLBLGen, because it forces ECId foreign key and EBId foreign key defined in EA entity and not in the particular descendants.

Missed I something?

thx for help.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 04-Aug-2008 15:59:57   

Is this a target per entity or a target per hierarchy inheritance? If this is target per hierarchy, you can use the designer to select which fields are mapped to which type.

Please specify the SQL DDL for these entities.

horo
User
Posts: 41
Joined: 01-Sep-2005
# Posted on: 31-Aug-2008 10:31:00   

Hi Walaa,

Sorry for the delay, but that day was just the first day of my vacation. Now I'am back.

Answers: - I try to create a target per hierarchy inheritance. - The DDL you've requested is appended to the end of this message.


More info:

I tried it keep as simple as possible. Three entities:

Entity 'Parameter': it is the base class. It has three attributes: - [ParameterId] [int] NOT NULL, - [ParameterType] [int] NOT NULL, - [ParameterValue] varchar NOT NULL,

The discriminator column will be the "ParameterType"


The first descendant entity 'AParameter' should have one more attribute:

  • [EntityAId] [int] NULL,

which is unfortunetelly a foreign key to EntityA (thats the problem)

An other descendant entity 'BParameter' (sibling of AParameter) should have one more attribute:

  • [EntityBId] [int] NULL,

which is unfortunetelly a foreign key to EntityB (thats the problem)

The problem is, if I declare the foreign constraint in SQL server for theese foreign keys, LLBLGen designer does not allow to push down foreign key attributes to descendants, it must be defined in the hierachy root.

If I do not define the foreign constraint in database then I am free to push EntityAId to AParameter descendant and push EntityBIdB to BParameter descendant, but I loose the ability to create an EntityA property in AParameter entity in my domain model. (and the inverse AParameters property in EntityA entity.

To generalize this problem in the terms of domain model (maybe I missed something):

Inherited properties can be simple or complex (complex means: Refers to an other entity) Specific properties, which are introduced by some descendant can be only simple, but no complex, which is a pretty huge limit, regarding a regular domain model.

How to accomplish this task, or is there any workaround?

thx for answers: horo


DDL:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EntityB]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[EntityB]( [EntityBId] [int] NOT NULL, [SomeData] varchar NULL, CONSTRAINT [PK_EntityB] PRIMARY KEY CLUSTERED ( [EntityBId] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] END GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EntityA]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[EntityA]( [EntityAId] [int] NOT NULL, [SomeData] varchar NULL, CONSTRAINT [PK_EntityA] PRIMARY KEY CLUSTERED ( [EntityAId] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] END GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Parameter]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[Parameter]( [ParameterId] [int] NOT NULL, [ParameterType] [int] NOT NULL, [ParameterValue] varchar NOT NULL, [EntityAId] [int] NULL, [EntityBId] [int] NULL, CONSTRAINT [PK_Parameter] PRIMARY KEY CLUSTERED ( [ParameterId] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] END GO IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Parameter_EntityA]') AND parent_object_id = OBJECT_ID(N'[dbo].[Parameter]')) ALTER TABLE [dbo].[Parameter] WITH CHECK ADD CONSTRAINT [FK_Parameter_EntityA] FOREIGN KEY([EntityAId]) REFERENCES [dbo].[EntityA] ([EntityAId]) GO IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Parameter_EntityB]') AND parent_object_id = OBJECT_ID(N'[dbo].[Parameter]')) ALTER TABLE [dbo].[Parameter] WITH CHECK ADD CONSTRAINT [FK_Parameter_EntityB] FOREIGN KEY([EntityBId]) REFERENCES [dbo].[EntityB] ([EntityBId])

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Aug-2008 22:20:34   

Try this:

  1. Remove EntityBId and EntityBId from Parameter Entity. This would add those fields to Parameter's Unmapped target fields collection.

  2. Create the target per hierarchy inheritance.

  3. Edit AParameter, go to [Entity fields] tab. Then go to [Unmapped target fields] tab. There, select EntityAId and push [Map selected target field(s)] button.

  4. Do the same for BParameter.

What LLBLGen version are you using?

David Elizondo | LLBLGen Support Team
horo
User
Posts: 41
Joined: 01-Sep-2005
# Posted on: 01-Sep-2008 17:28:55   

Hi Daelmo,

It works!

Thx for fast answer.