Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,14 @@ protected virtual void GenerateEntityTypeAnnotations([NotNull] IEntityType entit
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(stringBuilder, nameof(stringBuilder));

var annotations = entityType.GetAnnotations().ToArray();
var annotations = entityType.GetAnnotations().ToList();

var displayName = entityType.DisplayName();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth putting a comment for this?

if (entityType.Name != displayName)
{
annotations.Add(new Annotation(CoreAnnotationNames.DisplayName, displayName));
}

if (annotations.Any())
{
foreach (var annotation in annotations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public static class CoreAnnotationNames
{
public const string MaxLengthAnnotation = "MaxLength";
public const string ProductVersionAnnotation = "ProductVersion";
public const string DisplayName = "DisplayName";
}
}
90 changes: 51 additions & 39 deletions src/EntityFramework.Core/Metadata/Internal/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

namespace Microsoft.Data.Entity.Metadata.Internal
{
public class EntityType :
ConventionalAnnotatable,
IMutableEntityType,
ICanGetNavigations,
IPropertyCountsAccessor,
public class EntityType :
ConventionalAnnotatable,
IMutableEntityType,
ICanGetNavigations,
IPropertyCountsAccessor,
ISnapshotFactorySource
{
private static readonly char[] _simpleNameChars = { '.', '+' };
Expand Down Expand Up @@ -76,7 +76,7 @@ public EntityType([NotNull] string name, [NotNull] Model model, ConfigurationSou

_properties = new SortedDictionary<string, Property>(new PropertyComparer(this));
#if DEBUG
DebugName = DisplayName();
DebugName = EntityTypeExtensions.DisplayName(this);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.DisplayName() ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is the lesser of two evils..?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this

#endif
}

Expand Down Expand Up @@ -113,7 +113,7 @@ public virtual Type ClrType
|| GetDirectlyDerivedTypes().Any()
|| GetProperties().Any())
{
throw new InvalidOperationException(CoreStrings.EntityTypeInUse(DisplayName()));
throw new InvalidOperationException(CoreStrings.EntityTypeInUse(EntityTypeExtensions.DisplayName(this)));
}

_typeOrName = value;
Expand Down Expand Up @@ -253,23 +253,6 @@ public virtual string Name
}
}

public virtual string DisplayName()
{
if (ClrType != null)
{
return ClrType.DisplayName(false) ?? ParseSimpleName();
}
return ParseSimpleName();
}

private string ParseSimpleName()
{
var fullName = (string)_typeOrName;
var lastDot = fullName.LastIndexOfAny(_simpleNameChars);

return lastDot > 0 ? fullName.Substring(lastDot + 1) : fullName;
}

public override string ToString() => Name;

public virtual ConfigurationSource GetConfigurationSource() => _configurationSource;
Expand Down Expand Up @@ -304,7 +287,8 @@ public virtual Key SetPrimaryKey(
{
if (_baseType != null)
{
throw new InvalidOperationException(CoreStrings.DerivedEntityTypeKey(DisplayName(), _baseType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.DerivedEntityTypeKey(EntityTypeExtensions.DisplayName(this), _baseType.DisplayName()));
}

if (_primaryKey != null)
Expand Down Expand Up @@ -394,21 +378,29 @@ public virtual Key AddKey([NotNull] IReadOnlyList<Property> properties,

if (_baseType != null)
{
throw new InvalidOperationException(CoreStrings.DerivedEntityTypeKey(DisplayName(), _baseType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.DerivedEntityTypeKey(EntityTypeExtensions.DisplayName(this), _baseType.DisplayName()));
}

foreach (var property in properties)
{
if (FindProperty(property.Name) != property)
{
throw new ArgumentException(CoreStrings.KeyPropertiesWrongEntity(Property.Format(properties), DisplayName()));
throw new ArgumentException(
CoreStrings.KeyPropertiesWrongEntity(
Property.Format(properties),
EntityTypeExtensions.DisplayName(this)));
}
}

var key = FindKey(properties);
if (key != null)
{
throw new InvalidOperationException(CoreStrings.DuplicateKey(Property.Format(properties), DisplayName(), key.DeclaringEntityType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.DuplicateKey(
Property.Format(properties),
EntityTypeExtensions.DisplayName(this),
key.DeclaringEntityType.DisplayName()));
}

key = new Key(properties, configurationSource);
Expand Down Expand Up @@ -522,7 +514,11 @@ public virtual ForeignKey AddForeignKey(
var duplicateForeignKey = FindForeignKeysInHierarchy(properties, principalKey, principalEntityType).FirstOrDefault();
if (duplicateForeignKey != null)
{
throw new InvalidOperationException(CoreStrings.DuplicateForeignKey(Property.Format(properties), DisplayName(), duplicateForeignKey.DeclaringEntityType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.DuplicateForeignKey(
Property.Format(properties),
EntityTypeExtensions.DisplayName(this),
duplicateForeignKey.DeclaringEntityType.DisplayName()));
}

var foreignKey = new ForeignKey(properties, principalKey, this, principalEntityType, configurationSource ?? ConfigurationSource.Convention);
Expand Down Expand Up @@ -641,7 +637,7 @@ private ForeignKey RemoveForeignKey([NotNull] ForeignKey foreignKey)
{
foreignKey.DeclaringEntityType.RemoveNavigation(foreignKey.DependentToPrincipal.Name);
}

if (foreignKey.PrincipalToDependent != null)
{
foreignKey.PrincipalEntityType.RemoveNavigation(foreignKey.PrincipalToDependent.Name);
Expand Down Expand Up @@ -690,14 +686,20 @@ public virtual Navigation AddNavigation(
}

throw new InvalidOperationException(
CoreStrings.DuplicateNavigation(name, DisplayName(), duplicateNavigation.DeclaringEntityType.DisplayName()));
CoreStrings.DuplicateNavigation(
name,
EntityTypeExtensions.DisplayName(this),
duplicateNavigation.DeclaringEntityType.DisplayName()));
}

var duplicateProperty = FindPropertiesInHierarchy(name).FirstOrDefault();
if (duplicateProperty != null)
{
throw new InvalidOperationException(CoreStrings.ConflictingProperty(name, DisplayName(),
duplicateProperty.DeclaringEntityType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.ConflictingProperty(
name,
EntityTypeExtensions.DisplayName(this),
duplicateProperty.DeclaringEntityType.DisplayName()));
}

Debug.Assert(!GetNavigations().Any(n => (n.ForeignKey == foreignKey) && (n.IsDependentToPrincipal() == pointsToPrincipal)),
Expand Down Expand Up @@ -788,14 +790,21 @@ public virtual Index AddIndex([NotNull] IReadOnlyList<Property> properties,
{
if (FindProperty(property.Name) != property)
{
throw new ArgumentException(CoreStrings.IndexPropertiesWrongEntity(Property.Format(properties), DisplayName()));
throw new ArgumentException(
CoreStrings.IndexPropertiesWrongEntity(
Property.Format(properties),
EntityTypeExtensions.DisplayName(this)));
}
}

var duplicateIndex = FindIndexesInHierarchy(properties).FirstOrDefault();
if (duplicateIndex != null)
{
throw new InvalidOperationException(CoreStrings.DuplicateIndex(Property.Format(properties), DisplayName(), duplicateIndex.DeclaringEntityType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.DuplicateIndex(
Property.Format(properties),
EntityTypeExtensions.DisplayName(this),
duplicateIndex.DeclaringEntityType.DisplayName()));
}

var index = new Index(properties, this, configurationSource);
Expand Down Expand Up @@ -877,14 +886,17 @@ public virtual Property AddProperty([NotNull] string name,
if (duplicateProperty != null)
{
throw new InvalidOperationException(CoreStrings.DuplicateProperty(
name, DisplayName(), duplicateProperty.DeclaringEntityType.DisplayName()));
name, EntityTypeExtensions.DisplayName(this), duplicateProperty.DeclaringEntityType.DisplayName()));
}

var duplicateNavigation = FindNavigationsInHierarchy(name).FirstOrDefault();
if (duplicateNavigation != null)
{
throw new InvalidOperationException(CoreStrings.ConflictingNavigation(name, DisplayName(),
duplicateNavigation.DeclaringEntityType.DisplayName()));
throw new InvalidOperationException(
CoreStrings.ConflictingNavigation(
name,
EntityTypeExtensions.DisplayName(this),
duplicateNavigation.DeclaringEntityType.DisplayName()));
}

var property = new Property(name, this, configurationSource);
Expand Down Expand Up @@ -998,7 +1010,7 @@ public virtual void PropertyMetadataChanged()
public virtual Func<InternalEntityEntry, ISnapshot> RelationshipSnapshotFactory
=> LazyInitializer.EnsureInitialized(ref _relationshipSnapshotFactory, CreateRelationshipSnapshotFactory);

private Func<InternalEntityEntry, ISnapshot> CreateRelationshipSnapshotFactory()
private Func<InternalEntityEntry, ISnapshot> CreateRelationshipSnapshotFactory()
=> new RelationshipSnapshotFactoryFactory().Create(this);

public virtual Func<InternalEntityEntry, ISnapshot> OriginalValuesFactory
Expand Down
16 changes: 4 additions & 12 deletions src/EntityFramework.Core/Metadata/Internal/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ namespace Microsoft.Data.Entity.Metadata.Internal
public static class EntityTypeExtensions
{
public static string DisplayName([NotNull] this IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

if (entityType.ClrType != null)
{
return entityType.ClrType.DisplayName(false);
}

var lastDot = entityType.Name.LastIndexOfAny(new[] { '.', '+' });

return lastDot > 0 ? entityType.Name.Substring(lastDot + 1) : entityType.Name;
}
=> entityType.ClrType != null
? entityType.ClrType.DisplayName(fullName: false)
: (entityType[CoreAnnotationNames.DisplayName] as string
?? entityType.Name);

public static IEnumerable<IEntityType> GetAllBaseTypesInclusive([NotNull] this IEntityType entityType)
{
Expand Down
Loading