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 5a9820a commit c4d56ff
Showing 28 changed files with 261 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ protected virtual void Generate([NotNull] AddColumnOperation operation, [NotNull

builder.Append(")");

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

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

builder.Append(")");

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

@@ -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);
}
}

@@ -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);
}
}

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

builder.Append(")");

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

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

builder.Append(")");

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

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

builder.Append(")");

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

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

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

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

builder.Append(")");

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

@@ -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)
@@ -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(";");
@@ -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(";");
@@ -716,7 +716,7 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu

builder.Append(")");

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

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

builder.Append("})");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -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);
}
}

@@ -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);
}
}

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

builder.Append(")");

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

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

builder.Append(")");

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

@@ -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);
}
}

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

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

Original file line number Diff line number Diff line change
@@ -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);
@@ -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(
@@ -284,7 +284,7 @@ protected virtual void GenerateKey(

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

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

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

stringBuilder.Append(";");
@@ -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)
@@ -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(
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions src/EntityFramework.Core/Extensions/MutableModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

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

@@ -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
@@ -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;
@@ -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.
2 changes: 1 addition & 1 deletion src/EntityFramework.Core/Infrastructure/IAnnotatable.cs
Original file line number Diff line number Diff line change
@@ -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
@@ -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.
@@ -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 c4d56ff

Please sign in to comment.