protected virtual void SetIdentity( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); if (entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null) { foreach (var property in entityTypeBuilder.Metadata.GetDeclaredProperties()) { entityTypeBuilder.Property(property.Name, ConfigurationSource.Convention) ?.ValueGenerated(null, ConfigurationSource.Convention); } Property valueGeneratedOnAddProperty; if ((valueGeneratedOnAddProperty = ValueGeneratedOnAddProperty(properties, entityTypeBuilder.Metadata)) != null) { entityTypeBuilder.Property( valueGeneratedOnAddProperty.Name, ((IProperty)valueGeneratedOnAddProperty).ClrType, ConfigurationSource.Convention) ?.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention); } } }
protected virtual void SetIdentity( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); if (entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null) { foreach (var property in entityTypeBuilder.Metadata.Properties) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.StoreGeneratedPattern(null, ConfigurationSource.Convention); } if (properties.Count == 1) { var property = properties.First(); if (property.ClrType.IsInteger() && entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null && !property.IsForeignKey()) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.StoreGeneratedPattern(StoreGeneratedPattern.Identity, ConfigurationSource.Convention); } } } }
protected virtual void SetIdentity( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); if (entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null) { foreach (var property in entityTypeBuilder.Metadata.Properties) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.ValueGenerated(null, ConfigurationSource.Convention); } if (properties.Count == 1) { var property = properties.First(); var propertyType = property.ClrType.UnwrapNullableType(); if ((propertyType.IsInteger() || propertyType == typeof(Guid)) && entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null && !property.IsForeignKey(entityTypeBuilder.Metadata)) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention); } } } }
public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && entityType.IsDocumentRoot() && !entityType.IsQueryType) { var idProperty = entityTypeBuilder.Property(IdPropertyName, typeof(string), ConfigurationSource.Convention); idProperty.HasValueGenerator((_, __) => new StringValueGenerator(generateTemporaryValues: false), ConfigurationSource.Convention); entityTypeBuilder.HasKey(new[] { idProperty.Metadata }, ConfigurationSource.Convention); var jObjectProperty = entityTypeBuilder.Property(JObjectPropertyName, typeof(JObject), ConfigurationSource.Convention); } else { var idProperty = entityType.FindDeclaredProperty(IdPropertyName); if (idProperty != null) { var key = entityType.FindKey(idProperty); if (key != null) { entityType.Builder.RemoveKey(key, ConfigurationSource.Convention); } } var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName); if (jObjectProperty != null) { entityType.Builder.RemoveShadowPropertiesIfUnused(new[] { jObjectProperty }); } } return(entityTypeBuilder); }
public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { if (entityTypeBuilder.Metadata.BaseType == null) { var idProperty = entityTypeBuilder.Property(IdPropertyName, typeof(string), ConfigurationSource.Convention); idProperty.HasValueGenerator((_, __) => new StringValueGenerator(generateTemporaryValues: false), ConfigurationSource.Convention); entityTypeBuilder.HasKey(new[] { idProperty.Metadata }, ConfigurationSource.Convention); var jObjectProperty = entityTypeBuilder.Property(JObjectPropertyName, typeof(JObject), ConfigurationSource.Convention); } return(entityTypeBuilder); }
public virtual InternalPropertyBuilder Attach( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, ConfigurationSource configurationSource) { var newProperty = Metadata.DeclaringEntityType.FindProperty(Metadata.Name); Debug.Assert(newProperty != null); var newPropertyBuilder = entityTypeBuilder.Property(Metadata.Name, configurationSource); if (newProperty == Metadata) { return(newPropertyBuilder); } newPropertyBuilder.MergeAnnotationsFrom(this); var oldIsReadOnlyAfterSaveConfigurationSource = Metadata.GetIsReadOnlyAfterSaveConfigurationSource(); if (oldIsReadOnlyAfterSaveConfigurationSource.HasValue) { newPropertyBuilder.ReadOnlyAfterSave(Metadata.IsReadOnlyAfterSave, oldIsReadOnlyAfterSaveConfigurationSource.Value); } var oldIsReadOnlyBeforeSaveConfigurationSource = Metadata.GetIsReadOnlyBeforeSaveConfigurationSource(); if (oldIsReadOnlyBeforeSaveConfigurationSource.HasValue) { newPropertyBuilder.ReadOnlyBeforeSave(Metadata.IsReadOnlyBeforeSave, oldIsReadOnlyBeforeSaveConfigurationSource.Value); } var oldIsNullableConfigurationSource = Metadata.GetIsNullableConfigurationSource(); if (oldIsNullableConfigurationSource.HasValue) { newPropertyBuilder.IsRequired(!Metadata.IsNullable, oldIsNullableConfigurationSource.Value); } var oldIsConcurrencyTokenConfigurationSource = Metadata.GetIsConcurrencyTokenConfigurationSource(); if (oldIsConcurrencyTokenConfigurationSource.HasValue) { newPropertyBuilder.IsConcurrencyToken(Metadata.IsConcurrencyToken, oldIsConcurrencyTokenConfigurationSource.Value); } var oldRequiresValueGeneratorConfigurationSource = Metadata.GetRequiresValueGeneratorConfigurationSource(); if (oldRequiresValueGeneratorConfigurationSource.HasValue) { newPropertyBuilder.RequiresValueGenerator(Metadata.RequiresValueGenerator, oldRequiresValueGeneratorConfigurationSource.Value); } var oldValueGeneratedConfigurationSource = Metadata.GetValueGeneratedConfigurationSource(); if (oldValueGeneratedConfigurationSource.HasValue) { newPropertyBuilder.ValueGenerated(Metadata.ValueGenerated, oldValueGeneratedConfigurationSource.Value); } return(newPropertyBuilder); }
private void SetIdentity(InternalEntityTypeBuilder entityTypeBuilder, Property property) { var propertyBuilder = entityTypeBuilder.Property( property.Name, ((IProperty)property).ClrType, ConfigurationSource.Convention); propertyBuilder?.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention); propertyBuilder?.UseValueGenerator(true, ConfigurationSource.Convention); }
protected virtual void ConfigureKeyProperties([NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); foreach (var property in properties.Where(property => !entityTypeBuilder.Metadata.ForeignKeys.SelectMany(fk => fk.Properties).Contains(property))) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.GenerateValueOnAdd(true, ConfigurationSource.Convention); } // TODO: Nullable, Sequence // Issue #213 }
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); }
protected virtual void SetValueGeneration( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); foreach (var property in properties.Where( property => !entityTypeBuilder.Metadata.GetForeignKeys().SelectMany(fk => fk.Properties).Contains(property))) { entityTypeBuilder.Property(property.ClrType, property.Name, ConfigurationSource.Convention) ?.UseValueGenerator(true, ConfigurationSource.Convention); } }
public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { var entityType = entityTypeBuilder.Metadata; if (entityType.BaseType == null && entityType.IsDocumentRoot() && !entityType.IsKeyless) { var idProperty = entityTypeBuilder.Property(IdPropertyName, typeof(string), ConfigurationSource.Convention); idProperty.HasValueGenerator((_, __) => new IdValueGenerator(), ConfigurationSource.Convention); entityTypeBuilder.HasKey(new[] { idProperty.Metadata }, ConfigurationSource.Convention); var jObjectProperty = entityTypeBuilder.Property(JObjectPropertyName, typeof(JObject), ConfigurationSource.Convention); jObjectProperty.Cosmos(ConfigurationSource.Convention).ToProperty(""); jObjectProperty.ValueGenerated(ValueGenerated.OnAddOrUpdate, ConfigurationSource.Convention); } else { var idProperty = entityType.FindDeclaredProperty(IdPropertyName); if (idProperty != null) { var key = entityType.FindKey(idProperty); if (key != null) { entityType.Builder.RemoveKey(key, ConfigurationSource.Convention); } } var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName); if (jObjectProperty != null) { entityType.Builder.RemoveShadowPropertiesIfUnused(new[] { jObjectProperty }); } } return(entityTypeBuilder); }
private static void ConfigureDiscriminator(InternalEntityTypeBuilder entityTypeBuilder) { var propertyBuilder = entityTypeBuilder.Property("Discriminator", typeof(string), ConfigurationSource.Convention); propertyBuilder.IsRequired(true, ConfigurationSource.Convention); propertyBuilder.AfterSave(PropertySaveBehavior.Throw, ConfigurationSource.Convention); propertyBuilder.HasValueGenerator( (_, et) => new DiscriminatorValueGenerator(et.CosmosSql().DiscriminatorValue), ConfigurationSource.Convention); var entityType = entityTypeBuilder.Metadata; entityType.CosmosSql().DiscriminatorProperty = propertyBuilder.Metadata; entityType.CosmosSql().DiscriminatorValue = entityType.ShortName(); }
protected virtual void ConfigureValueGenerationStrategy([NotNull] InternalEntityTypeBuilder entityTypeBuilder, [NotNull] IReadOnlyList <Property> properties, bool generateValue) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); Check.NotNull(properties, nameof(properties)); if (entityTypeBuilder.Metadata.FindPrimaryKey(properties) != null && properties.Count == 1 && properties.First().ClrType.IsInteger() && properties.First().GenerateValueOnAdd == generateValue) { entityTypeBuilder.Property(properties.First().ClrType, properties.First().Name, ConfigurationSource.Convention) .Annotation(SqlServerAnnotationNames.Prefix + SqlServerAnnotationNames.ValueGeneration, generateValue ? SqlServerValueGenerationStrategy.Default.ToString() : null, ConfigurationSource.Convention); } }
/// <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.HasClrType()) { var primitiveProperties = entityType.ClrType.GetRuntimeProperties().Where(IsCandidatePrimitiveProperty); foreach (var propertyInfo in primitiveProperties) { entityTypeBuilder.Property(propertyInfo, ConfigurationSource.Convention); } } return(entityTypeBuilder); }
public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; if (entityType.HasClrType) { var primitiveProperties = entityType.ClrType.GetRuntimeProperties().Where(IsCandidatePrimitiveProperty); foreach (var propertyInfo in primitiveProperties) { entityTypeBuilder.Property(propertyInfo, ConfigurationSource.Convention); } } return entityTypeBuilder; }
public virtual InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder) { Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); var entityType = entityTypeBuilder.Metadata; // TODO: Honor [NotMapped] // Issue #107 if (entityType.HasClrType) { var primitiveProperties = entityType.ClrType.GetRuntimeProperties().Where(ConventionsPropertyInfoExtensions.IsCandidatePrimitiveProperty); foreach (var propertyInfo in primitiveProperties) { entityTypeBuilder.Property(propertyInfo, ConfigurationSource.Convention); } } return(entityTypeBuilder); }
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; } } }
/// <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 InternalPropertyBuilder Attach([NotNull] InternalEntityTypeBuilder entityTypeBuilder) { var newProperty = entityTypeBuilder.Metadata.FindProperty(Metadata.Name); InternalPropertyBuilder newPropertyBuilder; var configurationSource = Metadata.GetConfigurationSource(); var typeConfigurationSource = Metadata.GetTypeConfigurationSource(); if (newProperty != null && (newProperty.GetConfigurationSource().Overrides(configurationSource) || newProperty.GetTypeConfigurationSource().Overrides(typeConfigurationSource) || (Metadata.ClrType == newProperty.ClrType && Metadata.GetIdentifyingMemberInfo()?.Name == newProperty.GetIdentifyingMemberInfo()?.Name))) { newPropertyBuilder = newProperty.Builder; newProperty.UpdateConfigurationSource(configurationSource); if (typeConfigurationSource.HasValue) { newProperty.UpdateTypeConfigurationSource(typeConfigurationSource.Value); } } else { newPropertyBuilder = Metadata.GetIdentifyingMemberInfo() == null ? entityTypeBuilder.Property( Metadata.ClrType, Metadata.Name, configurationSource, Metadata.GetTypeConfigurationSource()) : entityTypeBuilder.Property(Metadata.GetIdentifyingMemberInfo(), configurationSource); } if (newProperty == Metadata) { return(newPropertyBuilder); } newPropertyBuilder.MergeAnnotationsFrom(Metadata); var oldBeforeSaveBehaviorConfigurationSource = Metadata.GetBeforeSaveBehaviorConfigurationSource(); if (oldBeforeSaveBehaviorConfigurationSource.HasValue) { newPropertyBuilder.BeforeSave( Metadata.GetBeforeSaveBehavior(), oldBeforeSaveBehaviorConfigurationSource.Value); } var oldAfterSaveBehaviorConfigurationSource = Metadata.GetAfterSaveBehaviorConfigurationSource(); if (oldAfterSaveBehaviorConfigurationSource.HasValue) { newPropertyBuilder.AfterSave( Metadata.GetAfterSaveBehavior(), oldAfterSaveBehaviorConfigurationSource.Value); } var oldIsNullableConfigurationSource = Metadata.GetIsNullableConfigurationSource(); if (oldIsNullableConfigurationSource.HasValue) { newPropertyBuilder.IsRequired(!Metadata.IsNullable, oldIsNullableConfigurationSource.Value); } var oldIsConcurrencyTokenConfigurationSource = Metadata.GetIsConcurrencyTokenConfigurationSource(); if (oldIsConcurrencyTokenConfigurationSource.HasValue) { newPropertyBuilder.IsConcurrencyToken( Metadata.IsConcurrencyToken, oldIsConcurrencyTokenConfigurationSource.Value); } var oldValueGeneratedConfigurationSource = Metadata.GetValueGeneratedConfigurationSource(); if (oldValueGeneratedConfigurationSource.HasValue) { newPropertyBuilder.ValueGenerated(Metadata.ValueGenerated, oldValueGeneratedConfigurationSource.Value); } var oldFieldInfoConfigurationSource = Metadata.GetFieldInfoConfigurationSource(); if (oldFieldInfoConfigurationSource.HasValue && newPropertyBuilder.CanSetField(Metadata.FieldInfo, oldFieldInfoConfigurationSource)) { newPropertyBuilder.HasField(Metadata.FieldInfo, oldFieldInfoConfigurationSource.Value); } return(newPropertyBuilder); }
/// <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 InternalPropertyBuilder Attach( [NotNull] InternalEntityTypeBuilder entityTypeBuilder, ConfigurationSource configurationSource) { var newProperty = entityTypeBuilder.Metadata.FindProperty(Metadata.Name); InternalPropertyBuilder newPropertyBuilder; var typeConfigurationSource = Metadata.GetTypeConfigurationSource(); if (newProperty != null && (newProperty.GetConfigurationSource().Overrides(configurationSource) || newProperty.GetTypeConfigurationSource().Overrides(typeConfigurationSource) || (Metadata.ClrType == newProperty.ClrType && Metadata.PropertyInfo?.Name == newProperty.PropertyInfo?.Name))) { newPropertyBuilder = newProperty.Builder; newProperty.UpdateConfigurationSource(configurationSource); if (typeConfigurationSource.HasValue) { newProperty.UpdateTypeConfigurationSource(typeConfigurationSource.Value); } } else { newPropertyBuilder = Metadata.PropertyInfo == null ? entityTypeBuilder.Property(Metadata.Name, Metadata.ClrType, configurationSource, Metadata.GetTypeConfigurationSource()) : entityTypeBuilder.Property(Metadata.PropertyInfo, configurationSource); } if (newProperty == Metadata) { return(newPropertyBuilder); } newPropertyBuilder.MergeAnnotationsFrom(this); var oldIsReadOnlyAfterSaveConfigurationSource = Metadata.GetIsReadOnlyAfterSaveConfigurationSource(); if (oldIsReadOnlyAfterSaveConfigurationSource.HasValue) { newPropertyBuilder.ReadOnlyAfterSave(Metadata.IsReadOnlyAfterSave, oldIsReadOnlyAfterSaveConfigurationSource.Value); } var oldIsReadOnlyBeforeSaveConfigurationSource = Metadata.GetIsReadOnlyBeforeSaveConfigurationSource(); if (oldIsReadOnlyBeforeSaveConfigurationSource.HasValue) { newPropertyBuilder.ReadOnlyBeforeSave(Metadata.IsReadOnlyBeforeSave, oldIsReadOnlyBeforeSaveConfigurationSource.Value); } var oldIsNullableConfigurationSource = Metadata.GetIsNullableConfigurationSource(); if (oldIsNullableConfigurationSource.HasValue) { newPropertyBuilder.IsRequired(!Metadata.IsNullable, oldIsNullableConfigurationSource.Value); } var oldIsConcurrencyTokenConfigurationSource = Metadata.GetIsConcurrencyTokenConfigurationSource(); if (oldIsConcurrencyTokenConfigurationSource.HasValue) { newPropertyBuilder.IsConcurrencyToken(Metadata.IsConcurrencyToken, oldIsConcurrencyTokenConfigurationSource.Value); } var oldValueGeneratedConfigurationSource = Metadata.GetValueGeneratedConfigurationSource(); if (oldValueGeneratedConfigurationSource.HasValue) { newPropertyBuilder.ValueGenerated(Metadata.ValueGenerated, oldValueGeneratedConfigurationSource.Value); } var oldIsStoreGeneratedAlwaysConfigurationSource = Metadata.GetIsStoreGeneratedAlwaysConfigurationSource(); if (oldIsStoreGeneratedAlwaysConfigurationSource.HasValue) { newPropertyBuilder.IsStoreGeneratedAlways(Metadata.IsStoreGeneratedAlways, oldIsStoreGeneratedAlwaysConfigurationSource.Value); } var oldFieldInfoConfigurationSource = Metadata.GetFieldInfoConfigurationSource(); if (oldFieldInfoConfigurationSource.HasValue) { newPropertyBuilder.HasFieldInfo(Metadata.FieldInfo, oldFieldInfoConfigurationSource.Value); } return(newPropertyBuilder); }
private void SetIdentity(InternalEntityTypeBuilder entityTypeBuilder, Property property) => entityTypeBuilder.Property( property.Name, ((IProperty)property).ClrType, ConfigurationSource.Convention) ?.ValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Convention);