Esempio n. 1
0
        private static void ProcessJObjectProperty(IConventionEntityTypeBuilder entityTypeBuilder)
        {
            var entityType = entityTypeBuilder.Metadata;

            if (entityType.BaseType == null &&
                !entityType.IsKeyless)
            {
                var jObjectProperty = entityTypeBuilder.Property(typeof(JObject), JObjectPropertyName);
                jObjectProperty?.ToJsonProperty("");
                jObjectProperty?.ValueGenerated(ValueGenerated.OnAddOrUpdate);
            }
            else
            {
                var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName);
                if (jObjectProperty != null)
                {
                    entityType.Builder.RemoveUnusedImplicitProperties(new[] { jObjectProperty });
                }
            }
        }
Esempio n. 2
0
        private static void ProcessIdProperty(IConventionEntityTypeBuilder entityTypeBuilder)
        {
            IConventionKey      newKey     = null;
            IConventionProperty idProperty = null;
            var entityType = entityTypeBuilder.Metadata;

            if (entityType.BaseType == null &&
                entityType.IsDocumentRoot() &&
                !entityType.IsKeyless)
            {
                idProperty = entityType.FindDeclaredProperty(DefaultIdPropertyName)
                             ?? entityType.GetDeclaredProperties().FirstOrDefault(p => p.GetJsonPropertyName() == IdPropertyJsonName)
                             ?? entityTypeBuilder.Property(typeof(string), DefaultIdPropertyName, setTypeConfigurationSource: false)
                             ?.ToJsonProperty(IdPropertyJsonName)?.Metadata;

                if (idProperty != null)
                {
                    if (idProperty.ClrType == typeof(string))
                    {
                        if (idProperty.IsPrimaryKey())
                        {
                            idProperty.Builder.HasValueGenerator((Type)null);
                        }
                        else
                        {
                            idProperty.Builder.HasValueGenerator((_, __) => new IdValueGenerator());
                        }
                    }

                    var partitionKey = entityType.GetPartitionKeyPropertyName();
                    if (partitionKey != null)
                    {
                        var partitionKeyProperty = entityType.FindProperty(partitionKey);
                        if (partitionKeyProperty == null)
                        {
                            newKey = entityTypeBuilder.HasKey(new[] { idProperty })?.Metadata;
                        }
                        else
                        {
                            if (entityType.FindKey(new[] { partitionKeyProperty, idProperty }) == null)
                            {
                                newKey = entityTypeBuilder.HasKey(new[] { idProperty, partitionKeyProperty })?.Metadata;
                            }

                            entityTypeBuilder.HasNoKey(new[] { idProperty });
                        }
                    }
                    else
                    {
                        newKey = entityTypeBuilder.HasKey(new[] { idProperty })?.Metadata;
                    }
                }
            }
            else
            {
                idProperty = entityType.FindDeclaredProperty(DefaultIdPropertyName);
            }

            if (idProperty != null &&
                idProperty.GetContainingKeys().Count() > (newKey == null ? 0 : 1))
            {
                foreach (var key in idProperty.GetContainingKeys().ToList())
                {
                    if (key != newKey)
                    {
                        key.DeclaringEntityType.Builder.HasNoKey(key);
                    }
                }
            }
        }
Esempio n. 3
0
        private static void Process(IConventionEntityTypeBuilder entityTypeBuilder)
        {
            IConventionKey      newKey     = null;
            IConventionProperty idProperty = null;
            var entityType = entityTypeBuilder.Metadata;

            if (entityType.BaseType == null &&
                entityType.IsDocumentRoot() &&
                !entityType.IsKeyless)
            {
                idProperty = entityTypeBuilder.Property(typeof(string), IdPropertyName, setTypeConfigurationSource: false)
                             ?.Metadata;

                if (idProperty != null)
                {
                    if (idProperty.ClrType == typeof(string))
                    {
                        idProperty.Builder.HasValueGenerator((_, __) => new IdValueGenerator());
                    }

                    var partitionKey = entityType.GetPartitionKeyPropertyName();
                    if (partitionKey != null)
                    {
                        var partitionKeyProperty = entityType.FindProperty(partitionKey);
                        if (partitionKeyProperty != null)
                        {
                            newKey = entityTypeBuilder.HasKey(new[] { idProperty, partitionKeyProperty })?.Metadata;
                        }
                    }
                    else
                    {
                        newKey = entityTypeBuilder.HasKey(new[] { idProperty })?.Metadata;
                    }
                }
            }
            else
            {
                idProperty = entityType.FindDeclaredProperty(IdPropertyName);
            }

            if (idProperty != null)
            {
                foreach (var key in idProperty.GetContainingKeys().ToList())
                {
                    if (key != newKey)
                    {
                        key.DeclaringEntityType.Builder.HasNoKey(key);
                    }
                }
            }

            if (entityType.BaseType == null &&
                !entityType.IsKeyless)
            {
                var jObjectProperty = entityTypeBuilder.Property(typeof(JObject), JObjectPropertyName);
                jObjectProperty.ToJsonProperty("");
                jObjectProperty.ValueGenerated(ValueGenerated.OnAddOrUpdate);
            }
            else
            {
                var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName);
                if (jObjectProperty != null)
                {
                    entityType.Builder.HasNoUnusedShadowProperties(new[] { jObjectProperty });
                }
            }
        }
        /// <inheritdoc />
        public virtual void ProcessEntityTypeAnnotationChanged(
            IConventionEntityTypeBuilder entityTypeBuilder,
            string name,
            IConventionAnnotation?annotation,
            IConventionAnnotation?oldAnnotation,
            IConventionContext <IConventionAnnotation> context)
        {
            if (name == SqlServerAnnotationNames.IsTemporal)
            {
                if (annotation?.Value as bool? == true)
                {
                    if (entityTypeBuilder.Metadata.GetTemporalPeriodStartPropertyName() == null)
                    {
                        entityTypeBuilder.HasPeriodStart(PeriodStartDefaultName);
                    }

                    if (entityTypeBuilder.Metadata.GetTemporalPeriodEndPropertyName() == null)
                    {
                        entityTypeBuilder.HasPeriodEnd(PeriodEndDefaultName);
                    }

                    foreach (var skipLevelNavigation in entityTypeBuilder.Metadata.GetSkipNavigations())
                    {
                        if (skipLevelNavigation.DeclaringEntityType.IsTemporal() &&
                            skipLevelNavigation.Inverse is IConventionSkipNavigation inverse &&
                            inverse.DeclaringEntityType.IsTemporal() &&
                            skipLevelNavigation.JoinEntityType is IConventionEntityType joinEntityType &&
                            joinEntityType.HasSharedClrType &&
                            !joinEntityType.IsTemporal() &&
                            joinEntityType.GetConfigurationSource() == ConfigurationSource.Convention)
                        {
                            joinEntityType.SetIsTemporal(true);
                        }
                    }
                }
                else
                {
                    entityTypeBuilder.HasPeriodStart(null);
                    entityTypeBuilder.HasPeriodEnd(null);
                }
            }

            if (name == SqlServerAnnotationNames.TemporalPeriodStartPropertyName ||
                name == SqlServerAnnotationNames.TemporalPeriodEndPropertyName)
            {
                if (oldAnnotation?.Value is string oldPeriodPropertyName)
                {
                    var oldPeriodProperty = entityTypeBuilder.Metadata.GetProperty(oldPeriodPropertyName);
                    entityTypeBuilder.RemoveUnusedImplicitProperties(new[] { oldPeriodProperty });

                    if (oldPeriodProperty.GetTypeConfigurationSource() == ConfigurationSource.Explicit)
                    {
                        if ((name == SqlServerAnnotationNames.TemporalPeriodStartPropertyName &&
                             oldPeriodProperty.GetDefaultValue() is DateTime start &&
                             start == DateTime.MinValue) ||
                            (name == SqlServerAnnotationNames.TemporalPeriodEndPropertyName &&
                             oldPeriodProperty.GetDefaultValue() is DateTime end &&
                             end == DateTime.MaxValue))
                        {
                            oldPeriodProperty.Builder.HasDefaultValue(null);
                        }
                    }
                }

                if (annotation?.Value is string periodPropertyName)
                {
                    var periodPropertyBuilder = entityTypeBuilder.Property(
                        typeof(DateTime),
                        periodPropertyName);

                    if (periodPropertyBuilder != null)
                    {
                        // set column name explicitly so that we don't try to uniquefy it to some other column
                        // in case another property is defined that maps to the same column
                        periodPropertyBuilder.HasColumnName(periodPropertyName);
                    }
                }
            }
        }