private void InitializeLazyMembers() { // A property that is part of a "unified key" has multiple incoming associations _isUnified = new Lazy <bool>( () => IncomingAssociations.Count > 1); _lookupEntity = new Lazy <Entity>( () => { // Detached properties are not lookup properties if (Entity == null) { return(null); } // Is this property a TypeId or DescriptorId on the table? If so, it's not a lookup property. if (DescriptorEntitySpecification.IsEdFiDescriptorEntity(Entity.Name) && PropertyName == Entity.Name + "Id") // TODO: Embedded convention - Types/descriptor properties use entity name + "Id" suffix { return(null); } var currentAssociation = IncomingAssociations.FirstOrDefault( x => x.AssociationType == AssociationViewType.FromBase || x.AssociationType == AssociationViewType.OneToOneIncoming || x.AssociationType == AssociationViewType.ManyToOne // Prevent processing self-recursive relationships with key unification on current property (prevent endless loop) && !(x.IsSelfReferencing && PropertyName.EqualsIgnoreCase( x.PropertyMappingByThisName[PropertyName] .OtherProperty.PropertyName))); if (currentAssociation == null) { return(null); } var currentThisProperty = this; while (currentAssociation != null) { var otherProperty = currentAssociation.PropertyMappingByThisName[currentThisProperty.PropertyName] .OtherProperty; if (ModelComparers.EntityProperty.Equals(currentThisProperty, otherProperty)) { throw new Exception( "Association property mapping endpoints refer to the same property."); } currentThisProperty = otherProperty; // Stop looking if we've hit a lookup table (Type or Descriptor) if (currentThisProperty.Entity.IsLookup) { break; } currentAssociation = otherProperty.IncomingAssociations.FirstOrDefault( x => x.AssociationType == AssociationViewType.FromBase || x.AssociationType == AssociationViewType.OneToOneIncoming || x.AssociationType == AssociationViewType.ManyToOne && !(x.IsSelfReferencing && currentThisProperty.PropertyName.EqualsIgnoreCase( x.PropertyMappingByThisName[ currentThisProperty .PropertyName] .OtherProperty .PropertyName))); } return (currentThisProperty.Entity.IsLookup && currentThisProperty.Entity.Aggregate != Entity.Aggregate ? currentThisProperty.Entity : null); }); }