Question

I am using EF5 as my ORM tool. I have a relationship between 3 tables in the DB as follows:

DB Model

Table 1 is the main table. It has a "1 to 0..1" relationship to Table 2. Table 2 has a "many to 1" relationship to Table 3.

Now when you import this into EF, it collapses (hides) Table 2. You get to Table 3 by doing something like:

Table1.Table3

Basically in my T4 template for generating code, I need to know if the relationship between Table 1 and Table 3 is hiding a middle table.

How can I do this?

It is easy for a many to many table link, as I can look at the navigation properties relationship multiplicity as follows:

if (navProperty.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many &&
    navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)

And I will know a table is missing inbetween, but with the model above, it becomes a many to 0..1 relationship, which may or may not be hiding a middle table.

Any ideas?

Was it helpful?

Solution

I managed to find a rough workaround for this, and is as follows:

bool collapsedTable = false;

string relationshipTypeName = navProperty.RelationshipType.Name;
var assocSet = container.BaseEntitySets.OfType<AssociationSet>()
                                       .Where(es => es.Name == relationshipTypeName && !es.ElementType.IsForeignKey)
                                       .FirstOrDefault();

if (assocSet != null)
{
    collapsedTable = true;
}

In case anyone hits a similar problem. It seems to fix the exact issue I was having...whether or not this is a 100% solution to it for 100% percent of cases, I am not sure. Any issues with it, and I will revisit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top