public IEnumerable <RelatedEntityInfo> GetRelatedEntities(CodeType codeType, Project project, IProjectTypeLocator projectTypeLocator)
            {
                var thisEntityPrimaryKeys = PrimaryKeyLocation.GetPrimaryKeys(codeType).ToList();

                var propertiesToConsider = (from property in codeType.VisibleMembers().OfType <CodeProperty>()
                                            where ((CodeElement)property).IsPublic() &&
                                            !thisEntityPrimaryKeys.Contains(property)      // Exclude its own primary keys, otherwise it will always be related to itself
                                            select property).ToList();

                foreach (var property in propertiesToConsider)
                {
                    var relatedEntityInfo = GetRelatedParentEntityInfo(property, propertiesToConsider, project, projectTypeLocator)
                                            ?? GetRelatedChildEntityInfo(property, project, projectTypeLocator);
                    if (relatedEntityInfo != null)
                    {
                        yield return(relatedEntityInfo);
                    }
                }
            }
Beispiel #2
0
        protected override void ProcessRecordCore()
        {
            if (string.IsNullOrEmpty(Type))
            {
                throw new InvalidOperationException("Specify a value for 'Type'.");
            }

            var project = SolutionManager.GetProject(string.IsNullOrEmpty(Project) ? SolutionManager.DefaultProjectName : Project);

            if (project == null)
            {
                WriteError(string.Format("Could not find project '{0}'", Project ?? string.Empty));
                return;
            }

            var foundClass           = _projectTypeLocator.FindUniqueType(project, Type);
            var primaryKeyProperties = PrimaryKeyLocation.GetPrimaryKeys(foundClass).ToList();

            switch (primaryKeyProperties.Count)
            {
            case 0:
                if (ErrorIfNotFound)
                {
                    WriteError(string.Format("Cannot find primary key property for type '{0}'. No properties appear to be primary keys.", foundClass.FullName));
                }
                break;

            case 1:
                WriteObject(primaryKeyProperties.Single().Name);
                break;

            default:
                if (ErrorIfNotFound)
                {
                    var primaryKeyPropertyNames = string.Join(", ", primaryKeyProperties.Select(x => x.Name));
                    WriteError(string.Format("Cannot find primary key property for type '{0}'. Multiple properties appear to be primary keys: {1}", foundClass.FullName, primaryKeyPropertyNames));
                }
                break;
            }
        }