Skip to content

Commit

Permalink
Add Code First API to configure the discriminator column
Browse files Browse the repository at this point in the history
Add API for setting annotations on internal builders that returns success status
  • Loading branch information
AndriySvyryd committed Aug 8, 2015
1 parent fd1ee9d commit 0f11939
Show file tree
Hide file tree
Showing 64 changed files with 1,575 additions and 604 deletions.
5 changes: 1 addition & 4 deletions src/EntityFramework.Core/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ protected internal virtual void OnModelCreating(ModelBuilder modelBuilder)
/// The number of state entries written to the underlying database.
/// </returns>
[DebuggerStepThrough]
public virtual int SaveChanges()
{
return SaveChanges(acceptAllChangesOnSuccess: true);
}
public virtual int SaveChanges() => SaveChanges(acceptAllChangesOnSuccess: true);

/// <summary>
/// Saves all changes made in this context to the underlying database.
Expand Down
8 changes: 3 additions & 5 deletions src/EntityFramework.Core/Metadata/Builders/PropertyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace Microsoft.Data.Entity.Metadata.Builders
/// </summary>
public class PropertyBuilder : IAccessor<Model>, IAccessor<InternalPropertyBuilder>
{
private readonly InternalPropertyBuilder _builder;

/// <summary>
/// <para>
/// Initializes a new instance of the <see cref="PropertyBuilder" /> class to configure a given
Expand All @@ -36,13 +34,13 @@ public PropertyBuilder([NotNull] InternalPropertyBuilder builder)
{
Check.NotNull(builder, nameof(builder));

_builder = builder;
Builder = builder;
}

/// <summary>
/// The internal builder being used to configure the property.
/// </summary>
InternalPropertyBuilder IAccessor<InternalPropertyBuilder>.Service => _builder;
InternalPropertyBuilder IAccessor<InternalPropertyBuilder>.Service => Builder;

/// <summary>
/// The property being configured.
Expand Down Expand Up @@ -157,6 +155,6 @@ public virtual PropertyBuilder ValueGeneratedOnAddOrUpdate()
return this;
}

private InternalPropertyBuilder Builder => this.GetService<InternalPropertyBuilder>();
private InternalPropertyBuilder Builder { get; }
}
}
42 changes: 18 additions & 24 deletions src/EntityFramework.Core/Metadata/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public virtual EntityType BaseType

var baseProperties = value.Properties.Select(p => p.Name).ToArray();
var propertyCollisions = FindPropertyCollisions(baseProperties);
// ReSharper disable once PossibleMultipleEnumeration
if (propertyCollisions.Any())
{
throw new InvalidOperationException(
Expand All @@ -134,7 +133,6 @@ public virtual EntityType BaseType

var baseNavigations = value.Navigations.Select(p => p.Name).ToArray();
var navigationCollisions = FindNavigationCollisions(baseNavigations);
// ReSharper disable once PossibleMultipleEnumeration
if (navigationCollisions.Any())
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -167,6 +165,8 @@ private bool InheritsFrom(EntityType entityType)
return false;
}

public virtual EntityType RootType() => (EntityType)((IEntityType)this).RootType();

public virtual string Name
{
get
Expand Down Expand Up @@ -368,6 +368,8 @@ public virtual Key FindKey([NotNull] IReadOnlyList<Property> properties)

return FindDeclaredKey(properties) ?? BaseType?.FindKey(properties);
}

public virtual IEnumerable<Key> GetDeclaredKeys() => _keys.Values;

public virtual Key FindDeclaredKey([NotNull] IReadOnlyList<Property> properties)
{
Expand Down Expand Up @@ -482,6 +484,8 @@ public virtual ForeignKey FindForeignKey([NotNull] IReadOnlyList<Property> prope
return FindDeclaredForeignKey(properties) ?? BaseType?.FindForeignKey(properties);
}

public virtual IEnumerable<ForeignKey> GetDeclaredForeignKeys() => _foreignKeys.Values;

public virtual ForeignKey FindDeclaredForeignKey([NotNull] IReadOnlyList<Property> properties)
{
Check.NotEmpty(properties, nameof(properties));
Expand All @@ -504,8 +508,7 @@ private IEnumerable<ForeignKey> FindDerivedForeignKeys(IEnumerable<IReadOnlyList

return this.GetDerivedTypes()
.SelectMany(et => et.GetDeclaredForeignKeys()
.Where(foreignKey => searchForeignKeys.Contains(foreignKey.Properties)))
.Cast<ForeignKey>();
.Where(foreignKey => searchForeignKeys.Contains(foreignKey.Properties)));
}

private IEnumerable<ForeignKey> FindForeignKeyCollisions(IReadOnlyList<Property>[] properties)
Expand Down Expand Up @@ -666,8 +669,8 @@ private IEnumerable<Navigation> FindDerivedNavigations(IEnumerable<string> names
.Cast<Navigation>();
}

private IEnumerable<Navigation> FindNavigationCollisions(string[] propertyNames)
=> FindNavigations(propertyNames).Concat(FindDerivedNavigations(propertyNames));
private IReadOnlyList<Navigation> FindNavigationCollisions(string[] propertyNames)
=> FindNavigations(propertyNames).Concat(FindDerivedNavigations(propertyNames)).ToList();

public virtual Navigation RemoveNavigation([NotNull] Navigation navigation)
{
Expand Down Expand Up @@ -749,6 +752,8 @@ public virtual Index FindIndex([NotNull] IReadOnlyList<Property> properties)
return FindDeclaredIndex(properties) ?? BaseType?.FindIndex(properties);
}

public virtual IEnumerable<Index> GetDeclaredIndexes() => _indexes.Values;

public virtual Index FindDeclaredIndex([NotNull] IReadOnlyList<Property> properties)
{
Check.NotEmpty(properties, nameof(properties));
Expand All @@ -771,8 +776,7 @@ private IEnumerable<Index> FindDerivedIndexes(IEnumerable<IReadOnlyList<Property

return this.GetDerivedTypes()
.SelectMany(et => et.GetDeclaredIndexes()
.Where(index => searchIndexes.Contains(index.Properties)))
.Cast<Index>();
.Where(index => searchIndexes.Contains(index.Properties)));
}

private IEnumerable<Index> FindIndexCollisions(IReadOnlyList<Property>[] properties)
Expand Down Expand Up @@ -902,21 +906,13 @@ public virtual Property FindDeclaredProperty([NotNull] string propertyName)
: null;
}

public virtual IEnumerable<Property> GetDeclaredProperties() => _properties.Values;

private IEnumerable<Property> FindProperties(IEnumerable<string> propertyNames)
=> propertyNames.Select(FindProperty).Where(p => p != null);

private IEnumerable<Property> FindDerivedProperties(IEnumerable<string> propertyNames)
{
var searchProperties = new HashSet<string>(propertyNames);

return this.GetDerivedTypes()
.SelectMany(et => et.GetDeclaredProperties()
.Where(property => searchProperties.Contains(property.Name)))
.Cast<Property>();
}

private IEnumerable<Property> FindPropertyCollisions(string[] propertyNames)
=> FindProperties(propertyNames).Concat(FindDerivedProperties(propertyNames));

private IReadOnlyList<IProperty> FindPropertyCollisions(string[] propertyNames)
=> FindProperties(propertyNames).Concat(this.FindDerivedProperties(propertyNames)).ToList();

public virtual Property RemoveProperty([NotNull] Property property)
{
Expand All @@ -925,9 +921,7 @@ public virtual Property RemoveProperty([NotNull] Property property)
Property removedProperty;
if (_properties.TryGetValue(property.Name, out removedProperty))
{
if (GetKeys().Any(k => k.Properties.Contains(property))
|| GetForeignKeys().Any(k => k.Properties.Contains(property))
|| Indexes.Any(i => i.Properties.Contains(property)))
if (property.IsInUse)
{
throw new InvalidOperationException(Strings.PropertyInUse(property.Name, Name));
}
Expand Down
17 changes: 16 additions & 1 deletion src/EntityFramework.Core/Metadata/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static IEnumerable<IEntityType> GetConcreteTypesInHierarchy([NotNull] thi
.Concat(entityType.GetDerivedTypes())
.Where(et => !et.IsAbstract());
}

public static IEnumerable<EntityType> GetConcreteTypesInHierarchy([NotNull] this EntityType entityType)
=> ((IEntityType)entityType).GetConcreteTypesInHierarchy().Cast<EntityType>();

private static IEnumerable<IEntityType> GetDerivedTypes(IModel model, IEntityType entityType)
{
Expand Down Expand Up @@ -162,6 +165,18 @@ public static IProperty GetProperty([NotNull] this IEntityType entityType, [NotN
return property;
}

public static IEnumerable<IProperty> FindDerivedProperties([NotNull] this IEntityType entityType, [NotNull] IEnumerable<string> propertyNames)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(propertyNames, nameof(propertyNames));

var searchProperties = new HashSet<string>(propertyNames);

return entityType.GetDerivedTypes()
.SelectMany(et => et.GetDeclaredProperties()
.Where(property => searchProperties.Contains(property.Name)));
}

[NotNull]
public static INavigation GetNavigation([NotNull] this IEntityType entityType, [NotNull] string name)
{
Expand Down Expand Up @@ -191,7 +206,7 @@ public static IEnumerable<IPropertyBase> GetPropertiesAndNavigations(
return entityType.GetProperties().Concat<IPropertyBase>(entityType.GetNavigations());
}

public static IEnumerable<IForeignKey> GetReferencingForeignKeys([NotNull] this IEntityType entityType)
public static IEnumerable<IForeignKey> FindReferencingForeignKeys([NotNull] this IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

Expand Down
11 changes: 9 additions & 2 deletions src/EntityFramework.Core/Metadata/ForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public static bool IsSelfReferencing([NotNull] this IForeignKey foreignKey)
return foreignKey.DeclaringEntityType == foreignKey.PrincipalEntityType;
}

public static bool IsSelfReferencing([NotNull] this ForeignKey foreignKey)
{
Check.NotNull(foreignKey, nameof(foreignKey));

return foreignKey.DeclaringEntityType == foreignKey.PrincipalEntityType;
}

public static bool IsIntraHierarchical([NotNull] this IForeignKey foreignKey)
{
Check.NotNull(foreignKey, nameof(foreignKey));
Expand Down Expand Up @@ -105,7 +112,7 @@ public static INavigation FindNavigationFrom([NotNull] this IForeignKey foreignK

if (foreignKey.IsIntraHierarchical())
{
throw new InvalidOperationException(Strings.IntraHierarchicalAmbiguousNavigation(entityType.Name, Property.Format(foreignKey.Properties)));
throw new InvalidOperationException(Strings.IntraHierarchicalAmbiguousNavigation(entityType.Name, Property.Format(foreignKey.Properties), foreignKey.PrincipalEntityType, foreignKey.DeclaringEntityType));
}

return foreignKey.DeclaringEntityType.IsAssignableFrom(entityType)
Expand All @@ -130,7 +137,7 @@ public static INavigation FindNavigationTo([NotNull] this IForeignKey foreignKey

if (foreignKey.IsIntraHierarchical())
{
throw new InvalidOperationException(Strings.IntraHierarchicalAmbiguousNavigation(entityType.Name, Property.Format(foreignKey.Properties)));
throw new InvalidOperationException(Strings.IntraHierarchicalAmbiguousNavigation(entityType.Name, Property.Format(foreignKey.Properties), foreignKey.PrincipalEntityType, foreignKey.DeclaringEntityType));
}

return foreignKey.DeclaringEntityType.IsAssignableFrom(entityType)
Expand Down
Loading

0 comments on commit 0f11939

Please sign in to comment.