Ejemplo n.º 1
0
        private void ProcessOtherEndOfReference(AssociationInformation info, Entity toEntity, Reference reference)
        {
            var potentialOtherEnds =
                associationInformation.Where(inf => inf.ThisEntityName == info.OtherEntityName
                && inf.OtherEntityName == info.ThisEntityName);

            AssociationInformation otherEndInfo = null;

            if (potentialOtherEnds.Count() == 1)
                otherEndInfo = potentialOtherEnds.FirstOrDefault();
            else if (potentialOtherEnds.Count() > 1)
            {
                foreach (var pot in potentialOtherEnds)
                {
                    if (info.ForeignKeyColumnNames.Count == pot.ForeignKeyColumnNames.Count)
                    {
                        bool found = true;

                        for (int i = 0; i < info.ForeignKeyColumnNames.Count; i++)
                        {
                            if (pot.ForeignKeyColumnNames[i] != info.ForeignKeyColumnNames[i])
                            {
                                found = false;
                                break;
                            }
                        }
                        if (found)
                        {
                            otherEndInfo = pot;
                            break;
                        }
                    }
                }
                //log.Error("While processing references, ");
            }
            reference.Entity2 = toEntity;

            if (info.ForeignKeyColumnNames.Count == 0 && otherEndInfo != null)
            {
                info.ForeignKeyColumnNames = otherEndInfo.ForeignKeyColumnNames;
                info.ForeignKeyBelongsToThisTable = false;
            }
            if (otherEndInfo == null)
            {
                reference.End2Enabled = false;
                Cardinality cardinality;

                if (info.AssociationTableName != null && !string.IsNullOrEmpty(info.AssociationTableName.TableName))
                    cardinality = Cardinality.Many;
                else if (info.HasUniqueConstraint)
                    cardinality = Cardinality.One;
                else
                    cardinality = Cardinality.Many;

                reference.Cardinality2 = cardinality;
            }
            else
            {
                reference.End2Enabled = true;
                reference.End2Name = otherEndInfo.PropertyName;
                reference.Cardinality2 = otherEndInfo.Cardinality;
                reference.SetEnd2AssociationType(otherEndInfo.AssociationType);
                reference.SetEnd2IndexColumnName(otherEndInfo.IndexColumn);
                reference.SetEnd2SqlWhereClause(otherEndInfo.WhereClause);
                reference.SetReferenceEnd2FetchMode(otherEndInfo.FetchMode);
                reference.SetReferenceEnd2CollectionFetchMode(otherEndInfo.CollectionFetchMode);
                reference.SetReferenceEnd2Insert(otherEndInfo.Insert);
                reference.SetReferenceEnd2Update(otherEndInfo.Update);
                reference.SetReferenceEnd2Inverse(otherEndInfo.Inverse);
                reference.SetReferenceEnd2Cascade(otherEndInfo.Cascade);
                reference.SetReferenceEnd2CollectionCascade(otherEndInfo.CollectionCascade);
                reference.SetReferenceEnd2Lazy(otherEndInfo.CollectionLazy);
                reference.SetReferenceEnd2OrderByIsAsc(otherEndInfo.OrderByIsAsc);

                if (!string.IsNullOrWhiteSpace(otherEndInfo.OrderByColumnName))
                {
                    var orderByProp = toEntity.Properties.SingleOrDefault(p => p.MappedColumn() != null && p.MappedColumn().Name.Equals(otherEndInfo.OrderByColumnName, StringComparison.InvariantCultureIgnoreCase));

                    if (orderByProp != null)
                        reference.SetReferenceEnd2OrderByProperty(orderByProp.Name);
                }
                //reference.SetReferenceEnd2OrderByProperty(otherEndInfo.OrderByColumnName);
                associationInformation.Remove(otherEndInfo);
            }
        }