Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Code First API to configure the discriminator column #2800

Merged
merged 1 commit into from
Aug 13, 2015
Merged
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
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; }
}
}
58 changes: 22 additions & 36 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();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unfortunate but can't think of an easy solution.


public virtual string Name
{
get
Expand Down Expand Up @@ -341,11 +341,6 @@ public virtual Key AddKey([NotNull] IReadOnlyList<Property> properties)
}

key = new Key(properties);
if (key.DeclaringEntityType != this)
{
throw new ArgumentException(Strings.KeyPropertiesWrongEntity(Property.Format(properties), Name));
}

_keys.Add(properties, key);

return key;
Expand All @@ -368,6 +363,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 +479,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 +503,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 @@ -653,6 +651,9 @@ public virtual Navigation FindDeclaredNavigation([NotNull] string name)
: null;
}

public virtual IEnumerable<Navigation> GetDeclaredNavigations()
=> _navigations.Values;

private IEnumerable<Navigation> FindNavigations(IEnumerable<string> names)
=> names.Select(FindNavigation).Where(p => p != null);

Expand All @@ -662,12 +663,11 @@ private IEnumerable<Navigation> FindDerivedNavigations(IEnumerable<string> names

return this.GetDerivedTypes()
.SelectMany(et => et.GetDeclaredNavigations()
.Where(navigation => searchNavigations.Contains(navigation.Name)))
.Cast<Navigation>();
.Where(navigation => searchNavigations.Contains(navigation.Name)));
}

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 @@ -720,11 +720,6 @@ public virtual Index AddIndex([NotNull] IReadOnlyList<Property> properties)
}

var index = new Index(properties);
if (index.DeclaringEntityType != this)
{
throw new ArgumentException(Strings.IndexPropertiesWrongEntity(Property.Format(properties), Name));
}

_indexes.Add(properties, index);

return index;
Expand All @@ -749,6 +744,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 +768,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 +898,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 +913,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
4 changes: 2 additions & 2 deletions src/EntityFramework.Core/Metadata/ForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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 +130,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