Page 1 of 1

Detecting foreign key in Entity Framework templates

Posted: Sun 30 Nov 2014 21:03
by billster6809
Given the following snippet, where primitiveProperties and navProperties are set as in your default EntityFramework template, how do I determine the value '????', which is whether a primitive property is also a foreign key?

I need to flag the foreign key properties from my custom method, WriteEdmColumnDefinition
<# foreach (EntityProperty edmProperty in primitiveProperties) {
if (!edmProperty.IsComplexType) {
bool isForeignKey = ???? }
WriteEdmColumnDefinition(edmProperty, isForeignKey, true);
}
#>

I had a commented-out piece of code, but it is flagged as invalid by the template processor, so I'm not sure where I got it from:
bool isForeignKey = navProperties.Any(np=>np.GetDependentProperties().Contains(edmProperty));

Re: Detecting foreign key in Entity Framework templates

Posted: Mon 01 Dec 2014 12:05
by MariiaI
Please open the POCO Entity template (the predefined one) in Entity Developer and look at this code (at line 1581):

Code: Select all

 bool isForeignKey = false;
    if (!property.IsComplexType) {
      foreach (RelationProperty relProp in ((Class)property.ParentClass).RelationProperties) {
        if (relProp.Association.Parent.Multiplicity != Multiplicity.Many &&
            relProp.Association.Parent == relProp &&
            relProp.Generate &&
            relProp.OppositeRelationProperty.Properties.Contains(property)) {
          isForeignKey = true;
          break;
        }
      }
    }

You can try implementing your scenario in a similar way.
Please tell us if this information helps.

Re: Detecting foreign key in Entity Framework templates

Posted: Tue 02 Dec 2014 14:45
by billster6809
That worked perfectly. Thank you!

Re: Detecting foreign key in Entity Framework templates

Posted: Wed 03 Dec 2014 06:11
by MariiaI
Glad to see that the issue was resolved. If you have any further questions, feel free to contact us.