Skip to content

Commit

Permalink
Add IMutableModel.AddEntityType(Type), GetOrAddEntityType(Type) and R…
Browse files Browse the repository at this point in the history
…emoveEntityType(Type) extension methods

Add IEntityType.ClrType setter
Change IAnnotatable.Annotations to GetAnnotations()
  • Loading branch information
AndriySvyryd committed Oct 30, 2015
1 parent fb4cad0 commit a187572
Show file tree
Hide file tree
Showing 28 changed files with 261 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected virtual void Generate([NotNull] AddColumnOperation operation, [NotNull

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ protected virtual void Generate([NotNull] AddForeignKeyOperation operation, [Not

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -223,7 +223,7 @@ protected virtual void Generate([NotNull] AddPrimaryKeyOperation operation, [Not
.Append(_code.Literal(operation.Columns))
.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -260,7 +260,7 @@ protected virtual void Generate([NotNull] AddUniqueConstraintOperation operation
.Append(_code.Literal(operation.Columns))
.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@ protected virtual void Generate([NotNull] AlterColumnOperation operation, [NotNu

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ protected virtual void Generate([NotNull] AlterSequenceOperation operation, [Not

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -431,7 +431,7 @@ protected virtual void Generate([NotNull] CreateIndexOperation operation, [NotNu

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -447,7 +447,7 @@ protected virtual void Generate([NotNull] EnsureSchemaOperation operation, [NotN

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -523,7 +523,7 @@ protected virtual void Generate([NotNull] CreateSequenceOperation operation, [No

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -611,7 +611,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

using (builder.Indent())
{
Annotations(column.Annotations, builder);
Annotations(column.GetAnnotations(), builder);
}

if (i != operation.Columns.Count - 1)
Expand Down Expand Up @@ -641,7 +641,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

using (builder.Indent())
{
Annotations(operation.PrimaryKey.Annotations, builder);
Annotations(operation.PrimaryKey.GetAnnotations(), builder);
}

builder.AppendLine(";");
Expand All @@ -658,7 +658,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

using (builder.Indent())
{
Annotations(uniqueConstraint.Annotations, builder);
Annotations(uniqueConstraint.GetAnnotations(), builder);
}

builder.AppendLine(";");
Expand Down Expand Up @@ -716,7 +716,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

builder.Append(")");

Annotations(foreignKey.Annotations, builder);
Annotations(foreignKey.GetAnnotations(), builder);
}

builder.AppendLine(";");
Expand All @@ -725,7 +725,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

builder.Append("})");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -752,7 +752,7 @@ protected virtual void Generate([NotNull] DropColumnOperation operation, [NotNul

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -779,7 +779,7 @@ protected virtual void Generate([NotNull] DropForeignKeyOperation operation, [No

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -806,7 +806,7 @@ protected virtual void Generate([NotNull] DropIndexOperation operation, [NotNull

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -833,7 +833,7 @@ protected virtual void Generate([NotNull] DropPrimaryKeyOperation operation, [No

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -849,7 +849,7 @@ protected virtual void Generate([NotNull] DropSchemaOperation operation, [NotNul

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -878,7 +878,7 @@ protected virtual void Generate([NotNull] DropSequenceOperation operation, [NotN

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -907,7 +907,7 @@ protected virtual void Generate([NotNull] DropTableOperation operation, [NotNull

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -934,7 +934,7 @@ protected virtual void Generate([NotNull] DropUniqueConstraintOperation operatio

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -968,7 +968,7 @@ protected virtual void Generate([NotNull] RenameColumnOperation operation, [NotN
.Append(_code.Literal(operation.NewName))
.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -1002,7 +1002,7 @@ protected virtual void Generate([NotNull] RenameIndexOperation operation, [NotNu
.Append(_code.Literal(operation.NewName))
.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -1045,7 +1045,7 @@ protected virtual void Generate([NotNull] RenameSequenceOperation operation, [No

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -1088,7 +1088,7 @@ protected virtual void Generate([NotNull] RenameTableOperation operation, [NotNu

builder.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down Expand Up @@ -1118,7 +1118,7 @@ protected virtual void Generate([NotNull] RestartSequenceOperation operation, [N
.Append(_code.Literal(operation.StartValue))
.Append(")");

Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand All @@ -1134,7 +1134,7 @@ protected virtual void Generate([NotNull] SqlOperation operation, [NotNull] Inde

using (builder.Indent())
{
Annotations(operation.Annotations, builder);
Annotations(operation.GetAnnotations(), builder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public virtual void Generate(
Check.NotNull(model, nameof(model));
Check.NotNull(stringBuilder, nameof(stringBuilder));

var annotations = model.Annotations.ToArray();
var annotations = model.GetAnnotations().ToArray();
if (annotations.Length != 0)
{
stringBuilder.Append(builderName);
Expand Down Expand Up @@ -240,7 +240,7 @@ protected virtual void GeneratePropertyAnnotations([NotNull] IProperty property,
Check.NotNull(property, nameof(property));
Check.NotNull(stringBuilder, nameof(stringBuilder));

GenerateAnnotations(property.Annotations.ToArray(), stringBuilder);
GenerateAnnotations(property.GetAnnotations().ToArray(), stringBuilder);
}

protected virtual void GenerateKeys(
Expand Down Expand Up @@ -284,7 +284,7 @@ protected virtual void GenerateKey(

using (stringBuilder.Indent())
{
GenerateAnnotations(key.Annotations.ToArray(), stringBuilder);
GenerateAnnotations(key.GetAnnotations().ToArray(), stringBuilder);
}

stringBuilder.Append(";");
Expand Down Expand Up @@ -324,7 +324,7 @@ protected virtual void GenerateIndex(
.Append(".IsUnique()");
}

GenerateAnnotations(index.Annotations.ToArray(), stringBuilder);
GenerateAnnotations(index.GetAnnotations().ToArray(), stringBuilder);
}

stringBuilder.Append(";");
Expand All @@ -335,7 +335,7 @@ protected virtual void GenerateEntityTypeAnnotations([NotNull] IEntityType entit
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(stringBuilder, nameof(stringBuilder));

var annotations = entityType.Annotations.ToArray();
var annotations = entityType.GetAnnotations().ToArray();
if (annotations.Any())
{
foreach (var annotation in annotations)
Expand Down Expand Up @@ -441,7 +441,7 @@ protected virtual void GenerateForeignKeyAnnotations([NotNull] IForeignKey forei
Check.NotNull(foreignKey, nameof(foreignKey));
Check.NotNull(stringBuilder, nameof(stringBuilder));

GenerateAnnotations(foreignKey.Annotations.ToArray(), stringBuilder);
GenerateAnnotations(foreignKey.GetAnnotations().ToArray(), stringBuilder);
}

protected virtual void GenerateAnnotations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private IEnumerable<IAnnotatable> GetAnnotatables(IModel model)

private IEnumerable<string> GetAnnotationNamespaces(IEnumerable<IAnnotatable> items)
=> from i in items
from a in i.Annotations
from a in i.GetAnnotations()
where a.Value != null && !IgnoredAnnotations.Contains(a.Name)
select a.Value.GetType().Namespace;
}
Expand Down
22 changes: 22 additions & 0 deletions src/EntityFramework.Core/Extensions/MutableModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Utilities;

Expand Down Expand Up @@ -34,5 +35,26 @@ public static IMutableEntityType GetOrAddEntityType([NotNull] this IMutableModel

return model.FindEntityType(name) ?? model.AddEntityType(name);
}

public static IMutableEntityType GetOrAddEntityType([NotNull] this IMutableModel model, [NotNull] Type type)
=> model.FindEntityType(type) ?? model.AddEntityType(type);

public static IMutableEntityType AddEntityType([NotNull] this IMutableModel model, [NotNull] Type type)
{
Check.NotNull(model, nameof(model));
Check.NotNull(type, nameof(type));

var entityType = model.AddEntityType(type.DisplayName());
entityType.ClrType = type;
return entityType;
}

public static IMutableEntityType RemoveEntityType([NotNull] this IMutableModel model, [NotNull] Type type)
{
Check.NotNull(model, nameof(model));
Check.NotNull(type, nameof(type));

return model.RemoveEntityType(type.DisplayName());
}
}
}
4 changes: 2 additions & 2 deletions src/EntityFramework.Core/Infrastructure/Annotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public virtual object this[[NotNull] string annotationName]
/// <summary>
/// Gets all annotations on the current object.
/// </summary>
public virtual IEnumerable<Annotation> Annotations
public virtual IEnumerable<Annotation> GetAnnotations()
=> _annotations.HasValue
? (IEnumerable<Annotation>)_annotations.Value
: ImmutableList<Annotation>.Empty;
Expand All @@ -144,7 +144,7 @@ private class AnnotationComparer : IComparer<IAnnotation>
/// <summary>
/// Gets all annotations on the current object.
/// </summary>
IEnumerable<IAnnotation> IAnnotatable.Annotations => Annotations;
IEnumerable<IAnnotation> IAnnotatable.GetAnnotations() => GetAnnotations();

/// <summary>
/// Gets the annotation with the given name, returning null if it does not exist.
Expand Down
2 changes: 1 addition & 1 deletion src/EntityFramework.Core/Infrastructure/IAnnotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public interface IAnnotatable
/// <summary>
/// Gets all annotations on the current object.
/// </summary>
IEnumerable<IAnnotation> Annotations { get; }
IEnumerable<IAnnotation> GetAnnotations();
}
}
4 changes: 2 additions & 2 deletions src/EntityFramework.Core/Metadata/IMutableAnnotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IMutableAnnotatable : IAnnotatable
/// <summary>
/// Gets all annotations on the current object.
/// </summary>
new IEnumerable<Annotation> Annotations { get; }
new IEnumerable<Annotation> GetAnnotations();

/// <summary>
/// Adds an annotation to this object. Throws if an annotation with the specified name already exists.
Expand All @@ -56,4 +56,4 @@ public interface IMutableAnnotatable : IAnnotatable
/// <returns> The annotation that was removed. </returns>
Annotation RemoveAnnotation([NotNull] string annotationName);
}
}
}
Loading

0 comments on commit a187572

Please sign in to comment.