Detecting foreign key in Entity Framework templates

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
billster6809
Posts: 29
Joined: Mon 27 Jan 2014 21:08

Detecting foreign key in Entity Framework templates

Post by billster6809 » Sun 30 Nov 2014 21:03

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));

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Detecting foreign key in Entity Framework templates

Post by MariiaI » Mon 01 Dec 2014 12:05

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.

billster6809
Posts: 29
Joined: Mon 27 Jan 2014 21:08

Re: Detecting foreign key in Entity Framework templates

Post by billster6809 » Tue 02 Dec 2014 14:45

That worked perfectly. Thank you!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Detecting foreign key in Entity Framework templates

Post by MariiaI » Wed 03 Dec 2014 06:11

Glad to see that the issue was resolved. If you have any further questions, feel free to contact us.

Post Reply