diff --git a/src/EFCore.Relational/Metadata/IRelationalModel.cs b/src/EFCore.Relational/Metadata/IRelationalModel.cs index c8ad086968b..2d1afdb1cd6 100644 --- a/src/EFCore.Relational/Metadata/IRelationalModel.cs +++ b/src/EFCore.Relational/Metadata/IRelationalModel.cs @@ -113,41 +113,48 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString).Append("RelationalModel: "); - - if (Collation != null) + try { - builder.AppendLine().Append(indentString).Append("Collation: ").Append(Collation); + builder.Append(indentString).Append("RelationalModel: "); + + if (Collation != null) + { + builder.AppendLine().Append(indentString).Append("Collation: ").Append(Collation); + } + + foreach (var table in Tables) + { + builder.AppendLine().Append(table.ToDebugString(options, indent + 2)); + } + + foreach (var view in Views) + { + builder.AppendLine().Append(view.ToDebugString(options, indent + 2)); + } + + foreach (var function in Functions) + { + builder.AppendLine().Append(function.ToDebugString(options, indent + 2)); + } + + foreach (var query in Queries) + { + builder.AppendLine().Append(query.ToDebugString(options, indent + 2)); + } + + foreach (var sequence in Sequences) + { + builder.AppendLine().Append(sequence.ToDebugString(options, indent + 2)); + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent)); + } } - - foreach (var table in Tables) - { - builder.AppendLine().Append(table.ToDebugString(options, indent + 2)); - } - - foreach (var view in Views) - { - builder.AppendLine().Append(view.ToDebugString(options, indent + 2)); - } - - foreach (var function in Functions) - { - builder.AppendLine().Append(function.ToDebugString(options, indent + 2)); - } - - foreach (var query in Queries) - { - builder.AppendLine().Append(query.ToDebugString(options, indent + 2)); - } - - foreach (var sequence in Sequences) - { - builder.AppendLine().Append(sequence.ToDebugString(options, indent + 2)); - } - - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore.Relational/Metadata/ISqlQuery.cs b/src/EFCore.Relational/Metadata/ISqlQuery.cs index 060168b1a85..48f9527605e 100644 --- a/src/EFCore.Relational/Metadata/ISqlQuery.cs +++ b/src/EFCore.Relational/Metadata/ISqlQuery.cs @@ -56,52 +56,59 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder - .Append(indentString) - .Append("SqlQuery: "); - - if (Schema != null) + try { builder - .Append(Schema) - .Append('.'); - } + .Append(indentString) + .Append("SqlQuery: "); - builder.Append(Name); - - if ((options & MetadataDebugStringOptions.SingleLine) == 0) - { - if (Sql != null) + if (Schema != null) { - builder.AppendLine().Append(indentString).Append(" Sql: "); - builder.AppendLine().Append(indentString).Append(new string(' ', 4)).Append(Sql); + builder + .Append(Schema) + .Append('.'); } - var mappings = EntityTypeMappings.ToList(); - if (mappings.Count != 0) + builder.Append(Name); + + if ((options & MetadataDebugStringOptions.SingleLine) == 0) { - builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); - foreach (var mapping in mappings) + if (Sql != null) { - builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Sql: "); + builder.AppendLine().Append(indentString).Append(new string(' ', 4)).Append(Sql); } - } - var columns = Columns.ToList(); - if (columns.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Columns: "); - foreach (var column in columns) + var mappings = EntityTypeMappings.ToList(); + if (mappings.Count != 0) { - builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); + foreach (var mapping in mappings) + { + builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + } } - } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) - { - builder.Append(AnnotationsToDebugString(indent + 2)); + var columns = Columns.ToList(); + if (columns.Count != 0) + { + builder.AppendLine().Append(indentString).Append(" Columns: "); + foreach (var column in columns) + { + builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + } + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); + } return builder.ToString(); } diff --git a/src/EFCore.Relational/Metadata/IStoreFunction.cs b/src/EFCore.Relational/Metadata/IStoreFunction.cs index a9f4f639bba..280ae3e5f63 100644 --- a/src/EFCore.Relational/Metadata/IStoreFunction.cs +++ b/src/EFCore.Relational/Metadata/IStoreFunction.cs @@ -71,72 +71,79 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder - .Append(indentString) - .Append("StoreFunction: "); - - if (ReturnType != null) - { - builder.Append(ReturnType); - } - else + try { - builder.Append(EntityTypeMappings.FirstOrDefault()?.EntityType.DisplayName() ?? ""); - } + builder + .Append(indentString) + .Append("StoreFunction: "); - builder.Append(' '); + if (ReturnType != null) + { + builder.Append(ReturnType); + } + else + { + builder.Append(EntityTypeMappings.FirstOrDefault()?.EntityType.DisplayName() ?? ""); + } - if (Schema != null) - { - builder - .Append(Schema) - .Append('.'); - } + builder.Append(' '); - builder.Append(Name); + if (Schema != null) + { + builder + .Append(Schema) + .Append('.'); + } - if (IsBuiltIn) - { - builder.Append(" IsBuiltIn"); - } + builder.Append(Name); - if ((options & MetadataDebugStringOptions.SingleLine) == 0) - { - var parameters = Parameters.ToList(); - if (parameters.Count != 0) + if (IsBuiltIn) { - builder.AppendLine().Append(indentString).Append(" Parameters: "); - foreach (var parameter in parameters) - { - builder.AppendLine().Append(parameter.ToDebugString(options, indent + 4)); - } + builder.Append(" IsBuiltIn"); } - var mappings = EntityTypeMappings.ToList(); - if (mappings.Count != 0) + if ((options & MetadataDebugStringOptions.SingleLine) == 0) { - builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); - foreach (var mapping in mappings) + var parameters = Parameters.ToList(); + if (parameters.Count != 0) { - builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Parameters: "); + foreach (var parameter in parameters) + { + builder.AppendLine().Append(parameter.ToDebugString(options, indent + 4)); + } } - } - var columns = Columns.ToList(); - if (columns.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Columns: "); - foreach (var column in columns) + var mappings = EntityTypeMappings.ToList(); + if (mappings.Count != 0) { - builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); + foreach (var mapping in mappings) + { + builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + } } - } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) - { - builder.Append(AnnotationsToDebugString(indent: indent + 2)); + var columns = Columns.ToList(); + if (columns.Count != 0) + { + builder.AppendLine().Append(indentString).Append(" Columns: "); + foreach (var column in columns) + { + builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + } + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent: indent + 2)); + } } } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); + } return builder.ToString(); } diff --git a/src/EFCore.Relational/Metadata/ITable.cs b/src/EFCore.Relational/Metadata/ITable.cs index 35472fa0fd9..8d7e45879ac 100644 --- a/src/EFCore.Relational/Metadata/ITable.cs +++ b/src/EFCore.Relational/Metadata/ITable.cs @@ -96,117 +96,124 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder - .Append(indentString) - .Append("Table: "); - - if (Schema != null) + try { builder - .Append(Schema) - .Append('.'); - } + .Append(indentString) + .Append("Table: "); + + if (Schema != null) + { + builder + .Append(Schema) + .Append('.'); + } - builder.Append(Name); + builder.Append(Name); - if (EntityTypeMappings.Any() - && EntityTypeMappings.First().EntityType is not RuntimeEntityType - && IsExcludedFromMigrations) - { - builder.Append(" ExcludedFromMigrations"); - } + if (EntityTypeMappings.Any() + && EntityTypeMappings.First().EntityType is not RuntimeEntityType + && IsExcludedFromMigrations) + { + builder.Append(" ExcludedFromMigrations"); + } - if (PrimaryKey == null) - { - builder.Append(" Keyless"); - } - else - { - if ((options & MetadataDebugStringOptions.SingleLine) == 0) + if (PrimaryKey == null) { - builder.AppendLine(); + builder.Append(" Keyless"); } + else + { + if ((options & MetadataDebugStringOptions.SingleLine) == 0) + { + builder.AppendLine(); + } - builder.Append(PrimaryKey.ToDebugString(options, indent + 2)); - } + builder.Append(PrimaryKey.ToDebugString(options, indent + 2)); + } - if ((options & MetadataDebugStringOptions.SingleLine) == 0 && Comment != null) - { - builder - .AppendLine() - .Append(indentString) - .AppendLine(" Comment:") - .Append(indentString) - .Append(Comment); - } + if ((options & MetadataDebugStringOptions.SingleLine) == 0 && Comment != null) + { + builder + .AppendLine() + .Append(indentString) + .AppendLine(" Comment:") + .Append(indentString) + .Append(Comment); + } - if ((options & MetadataDebugStringOptions.SingleLine) == 0) - { - var mappings = EntityTypeMappings.ToList(); - if (mappings.Count != 0) + if ((options & MetadataDebugStringOptions.SingleLine) == 0) { - builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); - foreach (var mapping in mappings) + var mappings = EntityTypeMappings.ToList(); + if (mappings.Count != 0) { - builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); + foreach (var mapping in mappings) + { + builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + } } - } - var columns = Columns.ToList(); - if (columns.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Columns: "); - foreach (var column in columns) + var columns = Columns.ToList(); + if (columns.Count != 0) { - builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Columns: "); + foreach (var column in columns) + { + builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + } } - } - var foreignKeyConstraints = ForeignKeyConstraints.ToList(); - if (foreignKeyConstraints.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" ForeignKeyConstraints: "); - foreach (var foreignKeyConstraint in foreignKeyConstraints) + var foreignKeyConstraints = ForeignKeyConstraints.ToList(); + if (foreignKeyConstraints.Count != 0) { - builder.AppendLine().Append(foreignKeyConstraint.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" ForeignKeyConstraints: "); + foreach (var foreignKeyConstraint in foreignKeyConstraints) + { + builder.AppendLine().Append(foreignKeyConstraint.ToDebugString(options, indent + 4)); + } } - } - var indexes = Indexes.ToList(); - if (indexes.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Indexes: "); - foreach (var index in indexes) + var indexes = Indexes.ToList(); + if (indexes.Count != 0) { - builder.AppendLine().Append(index.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Indexes: "); + foreach (var index in indexes) + { + builder.AppendLine().Append(index.ToDebugString(options, indent + 4)); + } } - } - var uniqueConstraints = UniqueConstraints.Where(uc => !uc.GetIsPrimaryKey()).ToList(); - if (uniqueConstraints.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" UniqueConstraints: "); - foreach (var uniqueConstraint in uniqueConstraints) + var uniqueConstraints = UniqueConstraints.Where(uc => !uc.GetIsPrimaryKey()).ToList(); + if (uniqueConstraints.Count != 0) { - builder.AppendLine().Append(uniqueConstraint.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" UniqueConstraints: "); + foreach (var uniqueConstraint in uniqueConstraints) + { + builder.AppendLine().Append(uniqueConstraint.ToDebugString(options, indent + 4)); + } } - } - var checkConstraints = CheckConstraints.ToList(); - if (checkConstraints.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Check constraints: "); - foreach (var checkConstraint in checkConstraints) + var checkConstraints = CheckConstraints.ToList(); + if (checkConstraints.Count != 0) { - builder.AppendLine().Append(checkConstraint.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Check constraints: "); + foreach (var checkConstraint in checkConstraints) + { + builder.AppendLine().Append(checkConstraint.ToDebugString(options, indent + 4)); + } } - } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) - { - builder.Append(AnnotationsToDebugString(indent + 2)); + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); + } return builder.ToString(); } diff --git a/src/EFCore.Relational/Metadata/ITableMapping.cs b/src/EFCore.Relational/Metadata/ITableMapping.cs index 71bceeb10a0..732c08d3756 100644 --- a/src/EFCore.Relational/Metadata/ITableMapping.cs +++ b/src/EFCore.Relational/Metadata/ITableMapping.cs @@ -40,49 +40,56 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString); - - var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; - if (singleLine) + try { - builder.Append("TableMapping: "); - } + builder.Append(indentString); - builder - .Append(EntityType.Name) - .Append(" - ") - .Append(Table.Name); + var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; + if (singleLine) + { + builder.Append("TableMapping: "); + } - builder.Append(" "); - if (!IncludesDerivedTypes) - { - builder.Append("!"); - } - builder.Append("IncludesDerivedTypes"); + builder + .Append(EntityType.Name) + .Append(" - ") + .Append(Table.Name); - if (IsSharedTablePrincipal != null) - { builder.Append(" "); - if (!IsSharedTablePrincipal.Value) + if (!IncludesDerivedTypes) { builder.Append("!"); } - builder.Append("IsSharedTablePrincipal"); - } + builder.Append("IncludesDerivedTypes"); - if (IsSplitEntityTypePrincipal != null) - { - builder.Append(" "); - if (!IsSplitEntityTypePrincipal.Value) + if (IsSharedTablePrincipal != null) { - builder.Append("!"); + builder.Append(" "); + if (!IsSharedTablePrincipal.Value) + { + builder.Append("!"); + } + builder.Append("IsSharedTablePrincipal"); } - builder.Append("IsSplitEntityTypePrincipal"); - } - if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + if (IsSplitEntityTypePrincipal != null) + { + builder.Append(" "); + if (!IsSplitEntityTypePrincipal.Value) + { + builder.Append("!"); + } + builder.Append("IsSplitEntityTypePrincipal"); + } + + if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } + } + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent + 2)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore.Relational/Metadata/IView.cs b/src/EFCore.Relational/Metadata/IView.cs index e97624639f5..d431934df2d 100644 --- a/src/EFCore.Relational/Metadata/IView.cs +++ b/src/EFCore.Relational/Metadata/IView.cs @@ -55,52 +55,59 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder - .Append(indentString) - .Append("View: "); - - if (Schema != null) + try { builder - .Append(Schema) - .Append('.'); - } + .Append(indentString) + .Append("View: "); - builder.Append(Name); - - if ((options & MetadataDebugStringOptions.SingleLine) == 0) - { - if (ViewDefinitionSql != null) + if (Schema != null) { - builder.AppendLine().Append(indentString).Append(" DefinitionSql: "); - builder.AppendLine().Append(indentString).Append(new string(' ', 4)).Append(ViewDefinitionSql); + builder + .Append(Schema) + .Append('.'); } - var mappings = EntityTypeMappings.ToList(); - if (mappings.Count != 0) + builder.Append(Name); + + if ((options & MetadataDebugStringOptions.SingleLine) == 0) { - builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); - foreach (var mapping in mappings) + if (ViewDefinitionSql != null) { - builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" DefinitionSql: "); + builder.AppendLine().Append(indentString).Append(new string(' ', 4)).Append(ViewDefinitionSql); } - } - var columns = Columns.ToList(); - if (columns.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Columns: "); - foreach (var column in columns) + var mappings = EntityTypeMappings.ToList(); + if (mappings.Count != 0) { - builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" EntityTypeMappings: "); + foreach (var mapping in mappings) + { + builder.AppendLine().Append(mapping.ToDebugString(options, indent + 4)); + } } - } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) - { - builder.Append(AnnotationsToDebugString(indent + 2)); + var columns = Columns.ToList(); + if (columns.Count != 0) + { + builder.AppendLine().Append(indentString).Append(" Columns: "); + foreach (var column in columns) + { + builder.AppendLine().Append(column.ToDebugString(options, indent + 4)); + } + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); + } return builder.ToString(); } diff --git a/src/EFCore/ChangeTracking/ChangeTrackerExtensions.cs b/src/EFCore/ChangeTracking/ChangeTrackerExtensions.cs index 9e929224019..aa1cd401f02 100644 --- a/src/EFCore/ChangeTracking/ChangeTrackerExtensions.cs +++ b/src/EFCore/ChangeTracking/ChangeTrackerExtensions.cs @@ -39,10 +39,17 @@ public static string ToDebugString( var builder = new StringBuilder(); var indentString = new string(' ', indent); - var stateManager = changeTracker.Context.GetService(); - foreach (var entry in stateManager.Entries.OrderBy(e => e, EntityEntryComparer.Instance)) + try { - builder.Append(indentString).AppendLine(entry.ToDebugString(options, indent)); + var stateManager = changeTracker.Context.GetService(); + foreach (var entry in stateManager.Entries.OrderBy(e => e, EntityEntryComparer.Instance)) + { + builder.Append(indentString).AppendLine(entry.ToDebugString(options, indent)); + } + } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Metadata/IReadOnlyEntityType.cs b/src/EFCore/Metadata/IReadOnlyEntityType.cs index 54983abbe34..cd07f87b88b 100644 --- a/src/EFCore/Metadata/IReadOnlyEntityType.cs +++ b/src/EFCore/Metadata/IReadOnlyEntityType.cs @@ -760,119 +760,126 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder - .Append(indentString) - .Append("EntityType: ") - .Append(DisplayName()); - - if (BaseType != null) + try { - builder.Append(" Base: ").Append(BaseType.DisplayName()); - } + builder + .Append(indentString) + .Append("EntityType: ") + .Append(DisplayName()); - if (HasSharedClrType) - { - builder.Append(" CLR Type: ").Append(ClrType.ShortDisplayName()); - } + if (BaseType != null) + { + builder.Append(" Base: ").Append(BaseType.DisplayName()); + } - if (IsAbstract()) - { - builder.Append(" Abstract"); - } + if (HasSharedClrType) + { + builder.Append(" CLR Type: ").Append(ClrType.ShortDisplayName()); + } - if (FindPrimaryKey() == null) - { - builder.Append(" Keyless"); - } + if (IsAbstract()) + { + builder.Append(" Abstract"); + } - if (IsOwned()) - { - builder.Append(" Owned"); - } + if (FindPrimaryKey() == null) + { + builder.Append(" Keyless"); + } - if (this is EntityType - && GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot) - { - builder.Append(" ChangeTrackingStrategy.").Append(GetChangeTrackingStrategy()); - } + if (IsOwned()) + { + builder.Append(" Owned"); + } - if ((options & MetadataDebugStringOptions.SingleLine) == 0) - { - var properties = GetDeclaredProperties().ToList(); - if (properties.Count != 0) + if (this is EntityType + && GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot) { - builder.AppendLine().Append(indentString).Append(" Properties: "); - foreach (var property in properties) - { - builder.AppendLine().Append(property.ToDebugString(options, indent + 4)); - } + builder.Append(" ChangeTrackingStrategy.").Append(GetChangeTrackingStrategy()); } - var navigations = GetDeclaredNavigations().ToList(); - if (navigations.Count != 0) + if ((options & MetadataDebugStringOptions.SingleLine) == 0) { - builder.AppendLine().Append(indentString).Append(" Navigations: "); - foreach (var navigation in navigations) + var properties = GetDeclaredProperties().ToList(); + if (properties.Count != 0) { - builder.AppendLine().Append(navigation.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Properties: "); + foreach (var property in properties) + { + builder.AppendLine().Append(property.ToDebugString(options, indent + 4)); + } } - } - var skipNavigations = GetDeclaredSkipNavigations().ToList(); - if (skipNavigations.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Skip navigations: "); - foreach (var skipNavigation in skipNavigations) + var navigations = GetDeclaredNavigations().ToList(); + if (navigations.Count != 0) { - builder.AppendLine().Append(skipNavigation.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Navigations: "); + foreach (var navigation in navigations) + { + builder.AppendLine().Append(navigation.ToDebugString(options, indent + 4)); + } } - } - var serviceProperties = GetDeclaredServiceProperties().ToList(); - if (serviceProperties.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Service properties: "); - foreach (var serviceProperty in serviceProperties) + var skipNavigations = GetDeclaredSkipNavigations().ToList(); + if (skipNavigations.Count != 0) { - builder.AppendLine().Append(serviceProperty.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Skip navigations: "); + foreach (var skipNavigation in skipNavigations) + { + builder.AppendLine().Append(skipNavigation.ToDebugString(options, indent + 4)); + } } - } - var keys = GetDeclaredKeys().ToList(); - if (keys.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Keys: "); - foreach (var key in keys) + var serviceProperties = GetDeclaredServiceProperties().ToList(); + if (serviceProperties.Count != 0) { - builder.AppendLine().Append(key.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Service properties: "); + foreach (var serviceProperty in serviceProperties) + { + builder.AppendLine().Append(serviceProperty.ToDebugString(options, indent + 4)); + } } - } - var fks = GetDeclaredForeignKeys().ToList(); - if (fks.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Foreign keys: "); - foreach (var fk in fks) + var keys = GetDeclaredKeys().ToList(); + if (keys.Count != 0) { - builder.AppendLine().Append(fk.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Keys: "); + foreach (var key in keys) + { + builder.AppendLine().Append(key.ToDebugString(options, indent + 4)); + } } - } - var indexes = GetDeclaredIndexes().ToList(); - if (indexes.Count != 0) - { - builder.AppendLine().Append(indentString).Append(" Indexes: "); - foreach (var index in indexes) + var fks = GetDeclaredForeignKeys().ToList(); + if (fks.Count != 0) { - builder.AppendLine().Append(index.ToDebugString(options, indent + 4)); + builder.AppendLine().Append(indentString).Append(" Foreign keys: "); + foreach (var fk in fks) + { + builder.AppendLine().Append(fk.ToDebugString(options, indent + 4)); + } } - } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) - { - builder.Append(AnnotationsToDebugString(indent: indent + 2)); + var indexes = GetDeclaredIndexes().ToList(); + if (indexes.Count != 0) + { + builder.AppendLine().Append(indentString).Append(" Indexes: "); + foreach (var index in indexes) + { + builder.AppendLine().Append(index.ToDebugString(options, indent + 4)); + } + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent: indent + 2)); + } } } + catch (Exception exception) + { + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); + } return builder.ToString(); } diff --git a/src/EFCore/Metadata/IReadOnlyForeignKey.cs b/src/EFCore/Metadata/IReadOnlyForeignKey.cs index a04edb587ab..70bf6d811dc 100644 --- a/src/EFCore/Metadata/IReadOnlyForeignKey.cs +++ b/src/EFCore/Metadata/IReadOnlyForeignKey.cs @@ -147,53 +147,60 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString); - - var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; - if (singleLine) + try { - builder.Append("ForeignKey: "); - } - - builder - .Append(DeclaringEntityType.DisplayName()) - .Append(' ') - .Append(Properties.Format()) - .Append(" -> ") - .Append(PrincipalEntityType.DisplayName()) - .Append(' ') - .Append(PrincipalKey.Properties.Format()); + builder.Append(indentString); - if (IsUnique) - { - builder.Append(" Unique"); - } + var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; + if (singleLine) + { + builder.Append("ForeignKey: "); + } - if (IsOwnership) - { - builder.Append(" Ownership"); - } - - if (PrincipalToDependent != null) - { - builder.Append(" ToDependent: ").Append(PrincipalToDependent.Name); - } - - if (DependentToPrincipal != null) - { - builder.Append(" ToPrincipal: ").Append(DependentToPrincipal.Name); - } - - if (DeleteBehavior != DeleteBehavior.NoAction) - { builder + .Append(DeclaringEntityType.DisplayName()) .Append(' ') - .Append(DeleteBehavior); + .Append(Properties.Format()) + .Append(" -> ") + .Append(PrincipalEntityType.DisplayName()) + .Append(' ') + .Append(PrincipalKey.Properties.Format()); + + if (IsUnique) + { + builder.Append(" Unique"); + } + + if (IsOwnership) + { + builder.Append(" Ownership"); + } + + if (PrincipalToDependent != null) + { + builder.Append(" ToDependent: ").Append(PrincipalToDependent.Name); + } + + if (DependentToPrincipal != null) + { + builder.Append(" ToPrincipal: ").Append(DependentToPrincipal.Name); + } + + if (DeleteBehavior != DeleteBehavior.NoAction) + { + builder + .Append(' ') + .Append(DeleteBehavior); + } + + if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } - - if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent + 2)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Metadata/IReadOnlyModel.cs b/src/EFCore/Metadata/IReadOnlyModel.cs index 0def3313c2c..e33e989e0c6 100644 --- a/src/EFCore/Metadata/IReadOnlyModel.cs +++ b/src/EFCore/Metadata/IReadOnlyModel.cs @@ -215,22 +215,29 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString).Append("Model: "); - - if (this is Model - && GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot) + try { - builder.Append(" ChangeTrackingStrategy.").Append(GetChangeTrackingStrategy()); - } + builder.Append(indentString).Append("Model: "); - foreach (var entityType in GetEntityTypes()) - { - builder.AppendLine().Append(entityType.ToDebugString(options, indent + 2)); - } + if (this is Model + && GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot) + { + builder.Append(" ChangeTrackingStrategy.").Append(GetChangeTrackingStrategy()); + } - if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + foreach (var entityType in GetEntityTypes()) + { + builder.AppendLine().Append(entityType.ToDebugString(options, indent + 2)); + } + + if ((options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent)); + } + } + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Metadata/IReadOnlyNavigation.cs b/src/EFCore/Metadata/IReadOnlyNavigation.cs index ed3310ec9eb..3553402dc5e 100644 --- a/src/EFCore/Metadata/IReadOnlyNavigation.cs +++ b/src/EFCore/Metadata/IReadOnlyNavigation.cs @@ -117,65 +117,72 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString); - - var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; - if (singleLine) - { - builder.Append($"Navigation: {DeclaringEntityType.DisplayName()}."); - } - - builder.Append(Name); - - var field = GetFieldName(); - if (field == null) - { - builder.Append(" (no field, "); - } - else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) - { - builder.Append($" ({field}, "); - } - else - { - builder.Append(" ("); - } - - builder.Append(ClrType.ShortDisplayName()).Append(')'); - - if (IsCollection) + try { - builder.Append(" Collection"); + builder.Append(indentString); + + var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; + if (singleLine) + { + builder.Append($"Navigation: {DeclaringEntityType.DisplayName()}."); + } + + builder.Append(Name); + + var field = GetFieldName(); + if (field == null) + { + builder.Append(" (no field, "); + } + else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) + { + builder.Append($" ({field}, "); + } + else + { + builder.Append(" ("); + } + + builder.Append(ClrType.ShortDisplayName()).Append(')'); + + if (IsCollection) + { + builder.Append(" Collection"); + } + + builder.Append(IsOnDependent ? " ToPrincipal " : " ToDependent "); + + builder.Append(TargetEntityType.DisplayName()); + + if (Inverse != null) + { + builder.Append(" Inverse: ").Append(Inverse.Name); + } + + if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) + { + builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); + } + + if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 + && ((AnnotatableBase)this).IsReadOnly) + { + var indexes = ((INavigation)this).GetPropertyIndexes(); + builder.Append(' ').Append(indexes.Index); + builder.Append(' ').Append(indexes.OriginalValueIndex); + builder.Append(' ').Append(indexes.RelationshipIndex); + builder.Append(' ').Append(indexes.ShadowIndex); + builder.Append(' ').Append(indexes.StoreGenerationIndex); + } + + if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } - - builder.Append(IsOnDependent ? " ToPrincipal " : " ToDependent "); - - builder.Append(TargetEntityType.DisplayName()); - - if (Inverse != null) - { - builder.Append(" Inverse: ").Append(Inverse.Name); - } - - if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) - { - builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); - } - - if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 - && ((AnnotatableBase)this).IsReadOnly) - { - var indexes = ((INavigation)this).GetPropertyIndexes(); - builder.Append(' ').Append(indexes.Index); - builder.Append(' ').Append(indexes.OriginalValueIndex); - builder.Append(' ').Append(indexes.RelationshipIndex); - builder.Append(' ').Append(indexes.ShadowIndex); - builder.Append(' ').Append(indexes.StoreGenerationIndex); - } - - if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent + 2)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Metadata/IReadOnlyProperty.cs b/src/EFCore/Metadata/IReadOnlyProperty.cs index 5aa4c42f8fd..e45fb65a726 100644 --- a/src/EFCore/Metadata/IReadOnlyProperty.cs +++ b/src/EFCore/Metadata/IReadOnlyProperty.cs @@ -292,113 +292,120 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString); - - var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; - if (singleLine) + try { - builder.Append($"Property: {DeclaringEntityType.DisplayName()}."); - } + builder.Append(indentString); - builder.Append(Name).Append(" ("); + var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; + if (singleLine) + { + builder.Append($"Property: {DeclaringEntityType.DisplayName()}."); + } - var field = GetFieldName(); - if (field == null) - { - builder.Append("no field, "); - } - else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) - { - builder.Append(field).Append(", "); - } + builder.Append(Name).Append(" ("); - builder.Append(ClrType.ShortDisplayName()).Append(')'); + var field = GetFieldName(); + if (field == null) + { + builder.Append("no field, "); + } + else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) + { + builder.Append(field).Append(", "); + } - if (IsShadowProperty()) - { - builder.Append(" Shadow"); - } + builder.Append(ClrType.ShortDisplayName()).Append(')'); - if (IsIndexerProperty()) - { - builder.Append(" Indexer"); - } + if (IsShadowProperty()) + { + builder.Append(" Shadow"); + } - if (!IsNullable) - { - builder.Append(" Required"); - } + if (IsIndexerProperty()) + { + builder.Append(" Indexer"); + } - if (IsPrimaryKey()) - { - builder.Append(" PK"); - } + if (!IsNullable) + { + builder.Append(" Required"); + } - if (IsForeignKey()) - { - builder.Append(" FK"); - } + if (IsPrimaryKey()) + { + builder.Append(" PK"); + } - if (IsKey() - && !IsPrimaryKey()) - { - builder.Append(" AlternateKey"); - } + if (IsForeignKey()) + { + builder.Append(" FK"); + } - if (IsIndex()) - { - builder.Append(" Index"); - } + if (IsKey() + && !IsPrimaryKey()) + { + builder.Append(" AlternateKey"); + } - if (IsConcurrencyToken) - { - builder.Append(" Concurrency"); - } + if (IsIndex()) + { + builder.Append(" Index"); + } - if (GetBeforeSaveBehavior() != PropertySaveBehavior.Save) - { - builder.Append(" BeforeSave:").Append(GetBeforeSaveBehavior()); - } + if (IsConcurrencyToken) + { + builder.Append(" Concurrency"); + } - if (GetAfterSaveBehavior() != PropertySaveBehavior.Save) - { - builder.Append(" AfterSave:").Append(GetAfterSaveBehavior()); - } + if (GetBeforeSaveBehavior() != PropertySaveBehavior.Save) + { + builder.Append(" BeforeSave:").Append(GetBeforeSaveBehavior()); + } - if (ValueGenerated != ValueGenerated.Never) - { - builder.Append(" ValueGenerated.").Append(ValueGenerated); - } + if (GetAfterSaveBehavior() != PropertySaveBehavior.Save) + { + builder.Append(" AfterSave:").Append(GetAfterSaveBehavior()); + } - if (GetMaxLength() != null) - { - builder.Append(" MaxLength(").Append(GetMaxLength()).Append(')'); - } + if (ValueGenerated != ValueGenerated.Never) + { + builder.Append(" ValueGenerated.").Append(ValueGenerated); + } - if (IsUnicode() == false) - { - builder.Append(" Ansi"); - } + if (GetMaxLength() != null) + { + builder.Append(" MaxLength(").Append(GetMaxLength()).Append(')'); + } - if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) - { - builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); - } + if (IsUnicode() == false) + { + builder.Append(" Ansi"); + } - if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 - && ((AnnotatableBase)this).IsReadOnly) - { - var indexes = ((IProperty)this).GetPropertyIndexes(); - builder.Append(' ').Append(indexes.Index); - builder.Append(' ').Append(indexes.OriginalValueIndex); - builder.Append(' ').Append(indexes.RelationshipIndex); - builder.Append(' ').Append(indexes.ShadowIndex); - builder.Append(' ').Append(indexes.StoreGenerationIndex); - } + if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) + { + builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); + } - if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 + && ((AnnotatableBase)this).IsReadOnly) + { + var indexes = ((IProperty)this).GetPropertyIndexes(); + builder.Append(' ').Append(indexes.Index); + builder.Append(' ').Append(indexes.OriginalValueIndex); + builder.Append(' ').Append(indexes.RelationshipIndex); + builder.Append(' ').Append(indexes.ShadowIndex); + builder.Append(' ').Append(indexes.StoreGenerationIndex); + } + + if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } + } + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent + 2)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Metadata/IReadOnlySkipNavigation.cs b/src/EFCore/Metadata/IReadOnlySkipNavigation.cs index 8c4dde37604..3ce55b9e5f5 100644 --- a/src/EFCore/Metadata/IReadOnlySkipNavigation.cs +++ b/src/EFCore/Metadata/IReadOnlySkipNavigation.cs @@ -62,63 +62,70 @@ string ToDebugString(MetadataDebugStringOptions options = MetadataDebugStringOpt var builder = new StringBuilder(); var indentString = new string(' ', indent); - builder.Append(indentString); - - var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; - if (singleLine) - { - builder.Append($"SkipNavigation: {DeclaringEntityType.DisplayName()}."); - } - - builder.Append(Name); - - var field = GetFieldName(); - if (field == null) - { - builder.Append(" (no field, "); - } - else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) - { - builder.Append($" ({field}, "); - } - else - { - builder.Append(" ("); - } - - builder.Append(ClrType.ShortDisplayName()).Append(')'); - - if (IsCollection) - { - builder.Append(" Collection"); - } - - builder.Append(TargetEntityType.DisplayName()); - - if (Inverse != null) - { - builder.Append(" Inverse: ").Append(Inverse.Name); - } - - if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) - { - builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); - } - - if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 - && ((AnnotatableBase)this).IsReadOnly) + try { - var indexes = ((ISkipNavigation)this).GetPropertyIndexes(); - builder.Append(' ').Append(indexes.Index); - builder.Append(' ').Append(indexes.OriginalValueIndex); - builder.Append(' ').Append(indexes.RelationshipIndex); - builder.Append(' ').Append(indexes.ShadowIndex); - builder.Append(' ').Append(indexes.StoreGenerationIndex); + builder.Append(indentString); + + var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; + if (singleLine) + { + builder.Append($"SkipNavigation: {DeclaringEntityType.DisplayName()}."); + } + + builder.Append(Name); + + var field = GetFieldName(); + if (field == null) + { + builder.Append(" (no field, "); + } + else if (!field.EndsWith(">k__BackingField", StringComparison.Ordinal)) + { + builder.Append($" ({field}, "); + } + else + { + builder.Append(" ("); + } + + builder.Append(ClrType.ShortDisplayName()).Append(')'); + + if (IsCollection) + { + builder.Append(" Collection"); + } + + builder.Append(TargetEntityType.DisplayName()); + + if (Inverse != null) + { + builder.Append(" Inverse: ").Append(Inverse.Name); + } + + if (GetPropertyAccessMode() != PropertyAccessMode.PreferField) + { + builder.Append(" PropertyAccessMode.").Append(GetPropertyAccessMode()); + } + + if ((options & MetadataDebugStringOptions.IncludePropertyIndexes) != 0 + && ((AnnotatableBase)this).IsReadOnly) + { + var indexes = ((ISkipNavigation)this).GetPropertyIndexes(); + builder.Append(' ').Append(indexes.Index); + builder.Append(' ').Append(indexes.OriginalValueIndex); + builder.Append(' ').Append(indexes.RelationshipIndex); + builder.Append(' ').Append(indexes.ShadowIndex); + builder.Append(' ').Append(indexes.StoreGenerationIndex); + } + + if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + { + builder.Append(AnnotationsToDebugString(indent + 2)); + } } - - if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) + catch (Exception exception) { - builder.Append(AnnotationsToDebugString(indent + 2)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } return builder.ToString(); diff --git a/src/EFCore/Properties/CoreStrings.Designer.cs b/src/EFCore/Properties/CoreStrings.Designer.cs index 23968555489..12d4a8e293e 100644 --- a/src/EFCore/Properties/CoreStrings.Designer.cs +++ b/src/EFCore/Properties/CoreStrings.Designer.cs @@ -574,6 +574,30 @@ public static string DbSetIncorrectGenericType(object? entityType, object? entit GetString("DbSetIncorrectGenericType", nameof(entityType), nameof(entityClrType), nameof(genericType)), entityType, entityClrType, genericType); + /// + /// Debug view threw {message}. Please report this at https://github.com/dotnet/efcore + /// + public static string DebugViewError(object? message) + => string.Format( + GetString("DebugViewError", nameof(message)), + message); + + /// + /// Error creating query expression: {message}. + /// + public static string DebugViewQueryExpressionError(object? message) + => string.Format( + GetString("DebugViewQueryExpressionError", nameof(message)), + message); + + /// + /// Error creating query string: {message}. + /// + public static string DebugViewQueryStringError(object? message) + => string.Format( + GetString("DebugViewQueryStringError", nameof(message)), + message); + /// /// The [DeleteBehavior] attribute may only be specified on navigation properties, and is not supported not on properties making up the foreign key. /// diff --git a/src/EFCore/Properties/CoreStrings.resx b/src/EFCore/Properties/CoreStrings.resx index b5f35ab4e21..1f6866bc7c4 100644 --- a/src/EFCore/Properties/CoreStrings.resx +++ b/src/EFCore/Properties/CoreStrings.resx @@ -327,6 +327,15 @@ Cannot create DbSet for entity type '{entityType}' since it is of type '{entityClrType}' but the generic type provided is of type '{genericType}'. + + Debug view threw {message}. Please report this at https://github.com/dotnet/efcore + + + Error creating query expression: {message}. + + + Error creating query string: {message}. + The [DeleteBehavior] attribute may only be specified on navigation properties, and is not supported not on properties making up the foreign key. diff --git a/src/EFCore/Query/Internal/QueryDebugView.cs b/src/EFCore/Query/Internal/QueryDebugView.cs index f44a5bdf700..27a00841d8f 100644 --- a/src/EFCore/Query/Internal/QueryDebugView.cs +++ b/src/EFCore/Query/Internal/QueryDebugView.cs @@ -38,7 +38,19 @@ public QueryDebugView( /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual string Expression - => _toExpressionString(); + { + get + { + try + { + return _toExpressionString(); + } + catch (Exception exception) + { + return CoreStrings.DebugViewQueryExpressionError(exception.Message); + } + } + } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -47,5 +59,17 @@ public virtual string Expression /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual string Query - => _toQueryString(); + { + get + { + try + { + return _toQueryString(); + } + catch (Exception exception) + { + return CoreStrings.DebugViewQueryStringError(exception.Message); + } + } + } } diff --git a/src/EFCore/Update/UpdateEntryExtensions.cs b/src/EFCore/Update/UpdateEntryExtensions.cs index 233c1ea99bc..b25ce46a2bc 100644 --- a/src/EFCore/Update/UpdateEntryExtensions.cs +++ b/src/EFCore/Update/UpdateEntryExtensions.cs @@ -88,182 +88,189 @@ public static string ToDebugString( var builder = new StringBuilder(); var indentString = new string(' ', indent); - var entry = (InternalEntityEntry)updateEntry; + try + { + var entry = (InternalEntityEntry)updateEntry; - var keyString = entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties); + var keyString = entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties); - builder - .Append(entry.EntityType.DisplayName()) - .Append(' ') - .Append(entry.SharedIdentityEntry != null ? "(Shared) " : "") - .Append(keyString) - .Append(' ') - .Append(entry.EntityState.ToString()); + builder + .Append(entry.EntityType.DisplayName()) + .Append(' ') + .Append(entry.SharedIdentityEntry != null ? "(Shared) " : "") + .Append(keyString) + .Append(' ') + .Append(entry.EntityState.ToString()); - if ((options & ChangeTrackerDebugStringOptions.IncludeProperties) != 0) - { - foreach (var property in entry.EntityType.GetProperties()) + if ((options & ChangeTrackerDebugStringOptions.IncludeProperties) != 0) { - builder.AppendLine().Append(indentString); + foreach (var property in entry.EntityType.GetProperties()) + { + builder.AppendLine().Append(indentString); - var currentValue = entry.GetCurrentValue(property); - builder - .Append(" ") - .Append(property.Name) - .Append(": "); + var currentValue = entry.GetCurrentValue(property); + builder + .Append(" ") + .Append(property.Name) + .Append(": "); - AppendValue(currentValue); + AppendValue(currentValue); - if (property.IsPrimaryKey()) - { - builder.Append(" PK"); - } - else if (property.IsKey()) - { - builder.Append(" AK"); - } + if (property.IsPrimaryKey()) + { + builder.Append(" PK"); + } + else if (property.IsKey()) + { + builder.Append(" AK"); + } - if (property.IsForeignKey()) - { - builder.Append(" FK"); - } + if (property.IsForeignKey()) + { + builder.Append(" FK"); + } - if (entry.IsModified(property)) - { - builder.Append(" Modified"); - } + if (entry.IsModified(property)) + { + builder.Append(" Modified"); + } - if (entry.HasTemporaryValue(property)) - { - builder.Append(" Temporary"); - } + if (entry.HasTemporaryValue(property)) + { + builder.Append(" Temporary"); + } - if (entry.IsUnknown(property)) - { - builder.Append(" Unknown"); - } + if (entry.IsUnknown(property)) + { + builder.Append(" Unknown"); + } - if (entry.HasOriginalValuesSnapshot - && property.GetOriginalValueIndex() != -1) - { - var originalValue = entry.GetOriginalValue(property); - if (!Equals(originalValue, currentValue)) + if (entry.HasOriginalValuesSnapshot + && property.GetOriginalValueIndex() != -1) { - builder.Append(" Originally "); - AppendValue(originalValue); + var originalValue = entry.GetOriginalValue(property); + if (!Equals(originalValue, currentValue)) + { + builder.Append(" Originally "); + AppendValue(originalValue); + } } } } - } - else - { - foreach (var alternateKey in entry.EntityType.GetKeys().Where(k => !k.IsPrimaryKey())) + else { - builder - .Append(" AK ") - .Append(entry.BuildCurrentValuesString(alternateKey.Properties)); - } + foreach (var alternateKey in entry.EntityType.GetKeys().Where(k => !k.IsPrimaryKey())) + { + builder + .Append(" AK ") + .Append(entry.BuildCurrentValuesString(alternateKey.Properties)); + } - foreach (var foreignKey in entry.EntityType.GetForeignKeys()) - { - builder - .Append(" FK ") - .Append(entry.BuildCurrentValuesString(foreignKey.Properties)); + foreach (var foreignKey in entry.EntityType.GetForeignKeys()) + { + builder + .Append(" FK ") + .Append(entry.BuildCurrentValuesString(foreignKey.Properties)); + } } - } - if ((options & ChangeTrackerDebugStringOptions.IncludeNavigations) != 0) - { - foreach (var navigation in entry.EntityType.GetNavigations() - .Concat(entry.EntityType.GetSkipNavigations())) + if ((options & ChangeTrackerDebugStringOptions.IncludeNavigations) != 0) { - builder.AppendLine().Append(indentString); + foreach (var navigation in entry.EntityType.GetNavigations() + .Concat(entry.EntityType.GetSkipNavigations())) + { + builder.AppendLine().Append(indentString); - var currentValue = entry.GetCurrentValue(navigation); - var targetType = navigation.TargetEntityType; + var currentValue = entry.GetCurrentValue(navigation); + var targetType = navigation.TargetEntityType; - builder - .Append(" ") - .Append(navigation.Name) - .Append(": "); + builder + .Append(" ") + .Append(navigation.Name) + .Append(": "); - if (currentValue == null) - { - builder.Append(""); - } - else if (navigation.IsCollection) - { - builder.Append('['); + if (currentValue == null) + { + builder.Append(""); + } + else if (navigation.IsCollection) + { + builder.Append('['); - const int maxRelatedToShow = 32; - var relatedEntities = ((IEnumerable)currentValue).Cast().Take(maxRelatedToShow + 1).ToList(); + const int maxRelatedToShow = 32; + var relatedEntities = ((IEnumerable)currentValue).Cast().Take(maxRelatedToShow + 1).ToList(); - for (var i = 0; i < relatedEntities.Count; i++) - { - if (i != 0) + for (var i = 0; i < relatedEntities.Count; i++) { - builder.Append(", "); + if (i != 0) + { + builder.Append(", "); + } + + if (i < 32) + { + AppendRelatedKey(targetType, relatedEntities[i]); + } + else + { + builder.Append("..."); + } } - if (i < 32) - { - AppendRelatedKey(targetType, relatedEntities[i]); - } - else - { - builder.Append("..."); - } + builder.Append(']'); + } + else + { + AppendRelatedKey(targetType, currentValue); } - - builder.Append(']'); + } + } + + void AppendValue(object? value) + { + if (value == null) + { + builder.Append(""); + } + else if (value.GetType().IsNumeric()) + { + builder.Append(value); + } + else if (value is byte[] bytes) + { + builder.AppendBytes(bytes); } else { - AppendRelatedKey(targetType, currentValue); + var stringValue = value.ToString(); + if (stringValue?.Length > 63) + { + stringValue = string.Concat(stringValue.AsSpan(0, 60), "..."); + } + + builder + .Append('\'') + .Append(stringValue) + .Append('\''); } } - } - return builder.ToString(); - - void AppendValue(object? value) - { - if (value == null) + void AppendRelatedKey(IEntityType targetType, object value) { - builder.Append(""); - } - else if (value.GetType().IsNumeric()) - { - builder.Append(value); - } - else if (value is byte[] bytes) - { - builder.AppendBytes(bytes); - } - else - { - var stringValue = value.ToString(); - if (stringValue?.Length > 63) - { - stringValue = string.Concat(stringValue.AsSpan(0, 60), "..."); - } + var otherEntry = entry.StateManager.TryGetEntry(value, targetType, throwOnTypeMismatch: false); - builder - .Append('\'') - .Append(stringValue) - .Append('\''); + builder.Append( + otherEntry == null + ? "" + : otherEntry.BuildCurrentValuesString(targetType.FindPrimaryKey()!.Properties)); } } - - void AppendRelatedKey(IEntityType targetType, object value) + catch (Exception exception) { - var otherEntry = entry.StateManager.TryGetEntry(value, targetType, throwOnTypeMismatch: false); - - builder.Append( - otherEntry == null - ? "" - : otherEntry.BuildCurrentValuesString(targetType.FindPrimaryKey()!.Properties)); + builder.AppendLine().AppendLine(CoreStrings.DebugViewError(exception.Message)); } + + return builder.ToString(); } ///