/// <summary>
 ///     Called after an entity type is added to the model if it has an attribute.
 /// </summary>
 /// <param name="entityTypeBuilder"> The builder for the entity type. </param>
 /// <param name="attribute"> The attribute. </param>
 /// <param name="context"> Additional information associated with convention execution. </param>
 protected override void ProcessEntityTypeAdded(
     IConventionEntityTypeBuilder entityTypeBuilder,
     KeylessAttribute attribute,
     IConventionContext <IConventionEntityTypeBuilder> context)
 {
     entityTypeBuilder.HasNoKey(fromDataAnnotation: true);
 }
Ejemplo 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);
                    }
                }
            }
        }
Ejemplo 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 })?.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(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 });
                }
            }
        }