This project is read-only.

Relationships Among Inherited Classes

Apr 5, 2011 at 6:26 PM

I'm running into a situation using inherited classes where I think I'm missing, or misusing, a configuration property.

My schema has the following classes in it:

item_container:
container_id (int, not null, database identity, primary key)
parent_id (int, null) -> has a self-referencing relationship with container_id; supports nesting/hierarchy of instances

topic : item_container
container_id (int, not null, primary key) -> linked to item_container's container_id field; establishes inheritance
forum_id (int, not null) -> linked to forum's container_id field

forum : item_container
container_id (int, not null, primary key) -> linked to item_container's container_id field; establishes inheritance

forum has a relationship with topic on forum.container_id = topic.forum_id

When I generate the schema using this structure the following shows up in the TopicQuery class (in the EntitiesQueries.Generated file):

/// <summary>
/// This is a mapping of the relationship with the Forum entity.
/// </summary>
[Association(ThisKey = "ContainerID", OtherKey = "ContainerID")]
public JumpForJoySoftware.T2T.EFDAL.ForumQuery Forum { get; private set; }

/// <summary>
/// This is a mapping of the relationship with the Posting entity.
/// </summary>
[Association(ThisKey = "ContainerID", OtherKey = "TopicID")]
public JumpForJoySoftware.T2T.EFDAL.PostingQuery PostingList { get; private set; }

/// <summary>
/// This is a mapping of the relationship with the Forum entity.
/// </summary>
[Association(ThisKey = "ForumID", OtherKey = "ContainerID")]
public JumpForJoySoftware.T2T.EFDAL.ForumQuery Forum { get; private set; }

There's a problem because the property Forum is being defined twice, once for a relationship between Topic.ForumID and Forum.ContainerID, and once for a relationship between Topic.ContainerID and Forum.ContainerID.

I think the second association, BTW, is spurious; there is no conceptual linkage between a Topic's ContainerID and a Forum's ContainerID. Looking through some other generated code, which has similar spurious-seeming associations, it looks like the code generator is linking child classes together based on their common "inheritance" field, which I don't understand.

I tested this theory by assigning role names to the inheritance relationships coming off of item_container. I assigned ForumBase to the relationship linking item_container to Forum, and TopicBase to the relationship linking item_container to Topic. Regenerating resulted in the following code from within TopicQuery (same file as above):

/// <summary>
/// This is a mapping of the relationship with the Forum entity. (Role: 'ForumBase')
/// </summary>
[Association(ThisKey = "ContainerID", OtherKey = "ContainerID")]
public JumpForJoySoftware.T2T.EFDAL.ForumQuery ForumBaseForum { get; private set; }

/// <summary>
/// This is a mapping of the relationship with the Posting entity.
/// </summary>
[Association(ThisKey = "ContainerID", OtherKey = "TopicID")]
public JumpForJoySoftware.T2T.EFDAL.PostingQuery PostingList { get; private set; }

/// <summary>
/// This is a mapping of the relationship with the Forum entity.
/// </summary>
[Association(ThisKey = "ForumID", OtherKey = "ContainerID")]
public JumpForJoySoftware.T2T.EFDAL.ForumQuery Forum { get; private set; }

Now there's no name collision, but I have to admit I'm confused about what the ForumBaseForum property is representing. It's a ForumQuery object, presumably linking an instance of Topic to an instance of Forum based on a common value for ContainerID. Yet there should never be a common value, since for both Forum and Topic ContainerID is linked to an autoincremented identity field in the base item_container class.

I also wonder where the association is between an instance of, say, Topic and its parent item_container instance. Maybe that doesn't need to be exposed since the Topic class is derived from item_container, but it seems like it would be more useful than the association defined by ForumBaseForum :).

So what am I doing wrong here?

- Mark