/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType != null || entityType.IsQueryType || !ConfigurationSource.Convention.Overrides(entityType.GetPrimaryKeyConfigurationSource())) { return(entityTypeBuilder); } IReadOnlyList <Property> keyProperties = null; var definingFk = entityType.FindDefiningNavigation()?.ForeignKey ?? entityType.FindOwnership(); if (definingFk?.IsUnique == false && definingFk.DeclaringEntityType == entityType) { entityTypeBuilder.PrimaryKey((IReadOnlyList <string>)null, ConfigurationSource.Convention); return(entityTypeBuilder); } if (definingFk?.IsUnique == true && definingFk.DeclaringEntityType == entityType) { keyProperties = definingFk.Properties; } if (keyProperties == null) { var candidateProperties = entityType.GetProperties().Where( p => !p.IsShadowProperty || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); keyProperties = (IReadOnlyList <Property>)DiscoverKeyProperties(entityType, candidateProperties); if (keyProperties.Count > 1) { _logger?.MultiplePrimaryKeyCandidates(keyProperties[0], keyProperties[1]); return(entityTypeBuilder); } } if (keyProperties.Count > 0) { entityTypeBuilder.PrimaryKey(keyProperties, ConfigurationSource.Convention); } return(entityTypeBuilder); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual InternalKeyBuilder Attach( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, ConfigurationSource?primaryKeyConfigurationSource) { var propertyNames = Metadata.Properties.Select(p => p.Name).ToList(); foreach (var propertyName in propertyNames) { if (entityTypeBuilder.Metadata.FindProperty(propertyName) == null) { return(null); } } var newKeyBuilder = entityTypeBuilder.HasKey(propertyNames, Metadata.GetConfigurationSource()); newKeyBuilder?.MergeAnnotationsFrom(Metadata); if (primaryKeyConfigurationSource.HasValue && newKeyBuilder != null) { var currentPrimaryKeyConfigurationSource = entityTypeBuilder.Metadata.GetPrimaryKeyConfigurationSource(); if (currentPrimaryKeyConfigurationSource?.Overrides(primaryKeyConfigurationSource.Value) != true) { entityTypeBuilder.PrimaryKey(newKeyBuilder.Metadata.Properties, primaryKeyConfigurationSource.Value); } } return(newKeyBuilder); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && ConfigurationSource.Convention.Overrides(entityType.GetPrimaryKeyConfigurationSource())) { var candidateProperties = entityType.GetProperties().Where(p => !p.IsShadowProperty || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); var keyProperties = DiscoverKeyProperties(entityType, candidateProperties).Select(p => p.Name).ToList(); if (keyProperties.Count > 1) { //TODO - log using Strings.MultiplePropertiesMatchedAsKeys() return(entityTypeBuilder); } if (keyProperties.Count > 0) { entityTypeBuilder.PrimaryKey(keyProperties, ConfigurationSource.Convention); } } return(entityTypeBuilder); }
public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; var keyProperties = DiscoverKeyProperties(entityType); if (keyProperties.Count != 0) { entityTypeBuilder.PrimaryKey(keyProperties.Select(p => p.Name).ToList(), ConfigurationSource.Convention); } return entityTypeBuilder; }
public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { if (null != entityTypeBuilder.Metadata.FindPrimaryKey() || null != entityTypeBuilder.Metadata.FindProperty(Id) || (entityTypeBuilder.Metadata.ClrType?.GetProperties().Any(x => x.Name == Id) ?? false)) { return(entityTypeBuilder); } entityTypeBuilder.Property("Id", typeof(int), ConfigurationSource.Convention) .Attach(entityTypeBuilder, ConfigurationSource.Convention); entityTypeBuilder.PrimaryKey(PK, ConfigurationSource.Convention); return(entityTypeBuilder); }
public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; var keyProperties = DiscoverKeyProperties(entityType); if (keyProperties.Count != 0) { entityTypeBuilder.PrimaryKey(keyProperties.Select(p => p.Name).ToList(), ConfigurationSource.Convention); } return(entityTypeBuilder); }
public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null) { var candidateProperties = entityType.GetProperties().Where(p => !((IProperty)p).IsShadowProperty || !entityTypeBuilder.CanRemoveProperty(p, ConfigurationSource.Convention)).ToList(); var keyProperties = DiscoverKeyProperties(entityType, candidateProperties); if (keyProperties.Count != 0) { entityTypeBuilder.PrimaryKey(keyProperties.Select(p => p.Name).ToList(), ConfigurationSource.Convention); } } return entityTypeBuilder; }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && ConfigurationSource.Convention.Overrides(entityType.GetPrimaryKeyConfigurationSource())) { IReadOnlyList <string> keyPropertyNames = null; if (entityType.HasDefiningNavigation()) { var definingFk = entityType.FindDefiningNavigation()?.ForeignKey; if (definingFk != null) { // Make sure that the properties won't be reuniquified definingFk.UpdateForeignKeyPropertiesConfigurationSource(ConfigurationSource.Convention); keyPropertyNames = definingFk.Properties.Select(p => p.Name).ToList(); } } if (keyPropertyNames == null) { var candidateProperties = entityType.GetProperties().Where( p => !p.IsShadowProperty || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); keyPropertyNames = DiscoverKeyProperties(entityType, candidateProperties).Select(p => p.Name).ToList(); if (keyPropertyNames.Count > 1) { //TODO - log using Strings.MultiplePropertiesMatchedAsKeys() return(entityTypeBuilder); } } if (keyPropertyNames.Any()) { entityTypeBuilder.PrimaryKey(keyPropertyNames, ConfigurationSource.Convention); } } return(entityTypeBuilder); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && entityType.FindPrimaryKey() == null) { var candidateProperties = entityType.GetProperties().Where(p => !p.IsShadowProperty || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); var keyProperties = DiscoverKeyProperties(entityType, candidateProperties); if (keyProperties.Count != 0) { entityTypeBuilder.PrimaryKey(keyProperties.Select(p => p.Name).ToList(), ConfigurationSource.Convention); } } return(entityTypeBuilder); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && ConfigurationSource.Convention.Overrides(entityType.GetPrimaryKeyConfigurationSource())) { IReadOnlyList <Property> keyProperties = null; var definingFk = entityType.FindDefiningNavigation()?.ForeignKey ?? entityType.FindOwnership(); if (definingFk != null && definingFk.IsUnique && definingFk.DeclaringEntityType == entityType) { // Make sure that the properties won't be reuniquified definingFk.UpdateForeignKeyPropertiesConfigurationSource(ConfigurationSource.Convention); keyProperties = definingFk.Properties.ToList(); } if (keyProperties == null) { var candidateProperties = entityType.GetProperties().Where( p => !p.IsShadowProperty || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); keyProperties = DiscoverKeyProperties(entityType, candidateProperties).ToList(); if (keyProperties.Count > 1) { _logger?.MultiplePrimaryKeyCandidates(keyProperties[0], keyProperties[1]); return(entityTypeBuilder); } } if (keyProperties.Any()) { entityTypeBuilder.PrimaryKey(keyProperties, ConfigurationSource.Convention); } } return(entityTypeBuilder); }
static ManyToManyTableMetadata BuildTableMetadata(InternalRelationshipBuilder relationshipBuilder, Navigation tableNavigation, Type collectionType) { EntityType parentEntityType = relationshipBuilder.Metadata.PrincipalEntityType; // get the clr type of the intermediate entity. EntityType tableEntityType = tableNavigation.GetTargetType(); InternalEntityTypeBuilder tableBuilder = relationshipBuilder.ModelBuilder.Entity(tableEntityType.ClrType, ConfigurationSource.DataAnnotation, true); // metadata for the intermediate table. IEnumerable <Navigation> tableNavigations = tableEntityType.GetNavigations(); Navigation end1Navigation = tableNavigations.Single(p => p.ClrType == parentEntityType.ClrType); Navigation end2Navigation = tableNavigations.Single(p => p.ClrType == collectionType); tableNavigation.Owned(ConfigurationSource.DataAnnotation); end1Navigation.Associated(ConfigurationSource.DataAnnotation); end2Navigation.Associated(ConfigurationSource.DataAnnotation); // if there is no FK, build one. if (tableEntityType.FindPrimaryKey() == null) { List <string> pkProps = tableEntityType.GetForeignKeys() .Where(fk => fk.PrincipalEntityType == parentEntityType || fk.PrincipalEntityType.ClrType == collectionType) .OrderBy(fk => fk.PrincipalEntityType != parentEntityType) .SelectMany(fk => fk.Properties) .Select(p => p.Name) .ToList(); tableBuilder.PrimaryKey(pkProps, ConfigurationSource.Explicit); } return(new ManyToManyTableMetadata { Getter = tableNavigation.Getter, Setter = tableNavigation.Setter, End1Getter = end1Navigation.GetGetter(), End1Setter = end1Navigation.GetSetter(), End2Getter = end2Navigation.GetGetter(), End2Setter = end2Navigation.GetSetter(), ItemType = tableEntityType.ClrType }); }
private static void MarkComplexTypes(InternalModelBuilder internalModelBuilder) { IEnumerable <EntityType> complexEntityTypes = internalModelBuilder .Metadata .GetEntityTypes() .Where(entityType => IsComplexType(entityType) && entityType.GetDerivedTypes().All(IsComplexType)) .ToList(); foreach (EntityType complexEntityType in complexEntityTypes) { complexEntityType.MongoDb().IsComplexType = true; if (complexEntityType.BaseType == null) { InternalEntityTypeBuilder internalEntityTypeBuilder = complexEntityType.Builder; InternalPropertyBuilder primaryKeyProperty = internalEntityTypeBuilder .Property( $"{complexEntityType.Name}TempId", typeof(int), ConfigurationSource.Convention); primaryKeyProperty.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention); internalEntityTypeBuilder.PrimaryKey( new[] { primaryKeyProperty.Metadata.Name }, ConfigurationSource.Convention); } IEnumerable <ForeignKey> referencingForeignKeys = internalModelBuilder .Metadata .GetEntityTypes() .SelectMany(entityType => entityType.GetForeignKeys()) .Where(foreignKey => foreignKey.PrincipalToDependent?.GetTargetType() == complexEntityType) .Concat(complexEntityType.GetReferencingForeignKeys()) .ToList(); foreach (ForeignKey referencingForeignKey in referencingForeignKeys) { referencingForeignKey.IsOwnership = true; } } }
public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { SortedList <int, PropertyInfo> pKey = new SortedList <int, PropertyInfo>(); foreach (Property property in entityTypeBuilder.Metadata.GetProperties()) { if (property.PropertyInfo != null) { PKeyAttribute pkeyAttribute = property.PropertyInfo.GetCustomAttribute <PKeyAttribute>(); if (pkeyAttribute != null) { pKey.Add(pkeyAttribute.Order, property.PropertyInfo); } } } if (pKey.Count > 0) { entityTypeBuilder.PrimaryKey(pKey.Values.ToArray(), ConfigurationSource.Explicit); } return(entityTypeBuilder); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType != null || entityType.IsKeyless || !ConfigurationSource.Convention.Overrides(entityType.GetPrimaryKeyConfigurationSource())) { return(entityTypeBuilder); } List <Property> keyProperties = null; var definingFk = entityType.FindDefiningNavigation()?.ForeignKey ?? entityType.FindOwnership(); if (definingFk != null && definingFk.DeclaringEntityType != entityType) { definingFk = null; } if (definingFk?.IsUnique == true) { keyProperties = definingFk.Properties.ToList(); } if (keyProperties == null) { var candidateProperties = entityType.GetProperties().Where( p => !p.IsShadowProperty() || !ConfigurationSource.Convention.Overrides(p.GetConfigurationSource())).ToList(); keyProperties = (List <Property>)DiscoverKeyProperties(entityType, candidateProperties); if (keyProperties.Count > 1) { Logger?.MultiplePrimaryKeyCandidates(keyProperties[0], keyProperties[1]); return(entityTypeBuilder); } } if (definingFk?.IsUnique == false) { if (keyProperties.Count == 0 || definingFk.Properties.Contains(keyProperties.First())) { var shadowProperty = entityType.FindPrimaryKey()?.Properties.Last(); if (shadowProperty == null || entityType.FindPrimaryKey().Properties.Count == 1 || definingFk.Properties.Contains(shadowProperty)) { shadowProperty = entityTypeBuilder.CreateUniqueProperty("Id", typeof(int), isRequired: true); } keyProperties.Clear(); keyProperties.Add(shadowProperty); } var extraProperty = keyProperties[0]; keyProperties.RemoveAt(0); keyProperties.AddRange(definingFk.Properties); keyProperties.Add(extraProperty); } if (keyProperties.Count > 0) { entityTypeBuilder.PrimaryKey(keyProperties, ConfigurationSource.Convention); } return(entityTypeBuilder); }
private static void ConfigureKeys(InternalEntityTypeBuilder entityTypeBuilder) { entityTypeBuilder.PrimaryKey(new[] { "Id" }, ConfigurationSource.Convention); }