示例#1
0
    /// <summary>
    ///     Validates the mapping/configuration of the model for cycles.
    /// </summary>
    /// <param name="model">The model to validate.</param>
    /// <param name="logger">The logger to use.</param>
    protected virtual void ValidateNoCycles(
        IModel model,
        IDiagnosticsLogger <DbLoggerCategory.Model.Validation> logger)
    {
        var graph = new Multigraph <IEntityType, IForeignKey>();

        foreach (var entityType in model.GetEntityTypes())
        {
            var primaryKey = entityType.FindPrimaryKey();
            if (primaryKey == null)
            {
                continue;
            }

            foreach (var foreignKey in entityType.GetForeignKeys())
            {
                var principalType = foreignKey.PrincipalEntityType;
                if (!foreignKey.PrincipalKey.IsPrimaryKey() ||
                    !PropertyListComparer.Instance.Equals(foreignKey.Properties, primaryKey.Properties) ||
                    foreignKey.PrincipalEntityType.IsAssignableFrom(entityType))
                {
                    continue;
                }

                graph.AddVertex(entityType);
                graph.AddVertex(principalType);
                graph.AddEdge(entityType, principalType, foreignKey);
            }
        }

        graph.TopologicalSort(
            tryBreakEdge: null,
            formatCycle: c => c.Select(d => d.Item1.DisplayName()).Join(" -> "),
            c => CoreStrings.IdentifyingRelationshipCycle(c));
    }
示例#2
0
        public void AddEdges_throws_on_verticies_not_in_the_graph()
        {
            var vertexOne = new Vertex {
                Id = 1
            };
            var vertexTwo = new Vertex {
                Id = 2
            };

            var edgeOne = new Edge {
                Id = 1
            };

            var graph = new Multigraph <Vertex, Edge>();

            graph.AddVertex(vertexOne);

            Assert.Equal(
                Strings.GraphDoesNotContainVertex(vertexTwo),
                Assert.Throws <InvalidOperationException>(() => graph.AddEdges(vertexOne, vertexTwo, new[] { edgeOne })).Message);

            Assert.Equal(
                Strings.GraphDoesNotContainVertex(vertexTwo),
                Assert.Throws <InvalidOperationException>(() => graph.AddEdges(vertexTwo, vertexOne, new[] { edgeOne })).Message);
        }
示例#3
0
    public void TopologicalSort_on_self_ref_can_break_cycle()
    {
        var vertexOne = new Vertex {
            Id = 1
        };

        var edgeOne = new Edge {
            Id = 1
        };

        var graph = new Multigraph <Vertex, Edge>();

        graph.AddVertex(vertexOne);

        // 1 -> {1}
        graph.AddEdge(vertexOne, vertexOne, edgeOne);

        Assert.Equal(
            new[] { vertexOne },
            graph.TopologicalSort(
                (from, to, edges) =>
                (from == vertexOne) &&
                (to == vertexOne) &&
                (edges.Intersect(new[] { edgeOne }).Count() == 1)).ToArray());
    }
示例#4
0
        public void AddVertex_adds_a_vertex()
        {
            var vertexOne = new Vertex {
                Id = 1
            };
            var vertexTwo = new Vertex {
                Id = 2
            };

            var graph = new Multigraph <Vertex, Edge>();

            graph.AddVertex(vertexOne);
            graph.AddVertex(vertexTwo);

            Assert.Equal(2, graph.Vertices.Count());
            Assert.Equal(2, graph.Vertices.Intersect(new[] { vertexOne, vertexTwo }).Count());
        }