Ejemplo n.º 1
0
        public virtual bool Apply(InternalKeyBuilder keyBuilder, Key previousPrimaryKey)
        {
            var entityTypeBuilder = keyBuilder.ModelBuilder.Entity(
                keyBuilder.Metadata.DeclaringEntityType.Name, ConfigurationSource.Convention);

            if (previousPrimaryKey != null)
            {
                foreach (var property in previousPrimaryKey.Properties)
                {
                    if (entityTypeBuilder.Metadata.FindProperty(property.Name) != null)
                    {
                        entityTypeBuilder.Property(property.Name, ConfigurationSource.Convention)
                            ?.ValueGenerated(null, ConfigurationSource.Convention);
                    }
                }
            }

            var valueGeneratedOnAddProperty = FindValueGeneratedOnAddProperty(keyBuilder.Metadata.Properties);
            if (valueGeneratedOnAddProperty != null
                && !valueGeneratedOnAddProperty.IsForeignKey(entityTypeBuilder.Metadata))
            {
                SetIdentity(entityTypeBuilder, valueGeneratedOnAddProperty);
            }

            return true;
        }
Ejemplo n.º 2
0
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            ConfigureKeyProperties(keyBuilder.ModelBuilder.Entity(keyBuilder.Metadata.EntityType.Name, ConfigurationSource.Convention),
                keyBuilder.Metadata.Properties);

            return keyBuilder;
        }
Ejemplo n.º 3
0
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            var entityTypeBuilder = keyBuilder.ModelBuilder.Entity(
                keyBuilder.Metadata.DeclaringEntityType.Name, ConfigurationSource.Convention);

            SetValueGeneration(entityTypeBuilder, keyBuilder.Metadata.Properties);

            return keyBuilder;
        }
        /// <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 InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            var key = keyBuilder.Metadata;

            foreach (var index in key.DeclaringEntityType.GetDerivedIndexesInclusive()
                     .Where(i => AreIndexedBy(i.Properties, i.IsUnique, key.Properties, true)).ToList())
            {
                index.DeclaringEntityType.Builder.RemoveIndex(index, ConfigurationSource.Convention);
            }

            return(keyBuilder);
        }
Ejemplo n.º 5
0
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            var entityTypeBuilder = keyBuilder.ModelBuilder.Entity(keyBuilder.Metadata.DeclaringEntityType.Name, ConfigurationSource.Convention);
            var properties = keyBuilder.Metadata.Properties;

            SetValueGeneration(entityTypeBuilder, properties);
            SetIdentity(entityTypeBuilder, properties);

            return keyBuilder;
        }
Ejemplo n.º 6
0
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            var entityTypeBuilder = keyBuilder.ModelBuilder.Entity(keyBuilder.Metadata.DeclaringEntityType.Name, ConfigurationSource.Convention);
            var properties        = keyBuilder.Metadata.Properties;

            SetValueGeneration(entityTypeBuilder, properties);
            SetIdentity(entityTypeBuilder, properties);

            return(keyBuilder);
        }
        public void OnKeyAdded_calls_apply_on_conventions_in_order(bool useBuilder)
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock <IKeyConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.NotNull(b);
                keyBuilder = new InternalKeyBuilder(b.Metadata, b.ModelBuilder);
                return(keyBuilder);
            });
            conventions.KeyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock <IKeyConvention>();

            nullConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.Same(keyBuilder, b);
                return(null);
            });
            conventions.KeyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock <IKeyConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.False(true);
                return(null);
            });
            conventions.KeyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);

            if (useBuilder)
            {
                Assert.Null(entityBuilder.HasKey(new List <string> {
                    "OrderId"
                }, ConfigurationSource.Convention));
            }
            else
            {
                var property = entityBuilder.Property("OrderId", ConfigurationSource.Convention).Metadata;
                property.IsNullable = false;
                Assert.Null(entityBuilder.Metadata.AddKey(property));
            }

            Assert.NotNull(keyBuilder);
        }
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            var key = keyBuilder.Metadata;

            ConfigureValueGenerationStrategy(
                keyBuilder.ModelBuilder.Entity(key.EntityType.Name, ConfigurationSource.Convention),
                key.Properties,
                true);

            return keyBuilder;
        }
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            var key = keyBuilder.Metadata;

            ConfigureValueGenerationStrategy(
                keyBuilder.ModelBuilder.Entity(key.EntityType.Name, ConfigurationSource.Convention),
                key.Properties,
                true);

            return(keyBuilder);
        }
Ejemplo n.º 10
0
        /// <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 bool Apply(InternalKeyBuilder keyBuilder, Key previousPrimaryKey)
        {
            if (previousPrimaryKey != null)
            {
                foreach (var property in previousPrimaryKey.Properties)
                {
                    property.Builder?.ValueGenerated(ValueGenerated.Never, ConfigurationSource.Convention);
                }
            }

            SetKeyValueGeneration(keyBuilder.Metadata.Properties, keyBuilder.Metadata.DeclaringEntityType);

            return(true);
        }
        /// <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 InternalKeyBuilder OnPrimaryKeySet([NotNull] InternalKeyBuilder keyBuilder, [CanBeNull] Key previousPrimaryKey)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            foreach (var keyConvention in _conventionSet.PrimaryKeySetConventions)
            {
                if (!keyConvention.Apply(keyBuilder, previousPrimaryKey))
                {
                    break;
                }
            }

            return(keyBuilder);
        }
Ejemplo n.º 12
0
        public virtual bool Apply(InternalKeyBuilder keyBuilder, Key previousPrimaryKey)
        {
            if (previousPrimaryKey != null)
            {
                foreach (var property in previousPrimaryKey.Properties)
                {
                    property.Builder?.ValueGenerated(ValueGenerated.Never, ConfigurationSource.Convention);
                    property.Builder?.RequiresValueGenerator(false, ConfigurationSource.Convention);
                }
            }

            SetIdentity(keyBuilder.Metadata.Properties, keyBuilder.Metadata.DeclaringEntityType);

            return true;
        }
        /// <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 InternalKeyBuilder OnKeyAdded([NotNull] InternalKeyBuilder keyBuilder)
        {
            Check.NotNull(keyBuilder, nameof(keyBuilder));

            foreach (var keyConvention in _conventionSet.KeyAddedConventions)
            {
                keyBuilder = keyConvention.Apply(keyBuilder);
                if (keyBuilder == null)
                {
                    break;
                }
            }

            return(keyBuilder);
        }
        /// <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 InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            var key = keyBuilder.Metadata;

            foreach (var foreignKey in key.DeclaringEntityType.GetDerivedForeignKeysInclusive().ToList())
            {
                if (key.Properties.All(p => foreignKey.Properties.Contains(p)) &&
                    (!foreignKey.IsUnique || foreignKey.DeclaringEntityType.BaseType != null))
                {
                    foreignKey.Builder.HasForeignKey((IReadOnlyList <Property>)null, ConfigurationSource.Convention);
                }
            }

            return(keyBuilder);
        }
        public void OnPrimaryKeySet_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder internalKeyBuilder = null;
            var convention = new Mock <IPrimaryKeyConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>(), It.IsAny <Key>()))
            .Returns <InternalKeyBuilder, Key>((b, t) =>
            {
                Assert.NotNull(b);
                Assert.Null(t);
                internalKeyBuilder = b;
                return(true);
            });
            conventions.PrimaryKeySetConventions.Add(convention.Object);

            var nullConvention = new Mock <IPrimaryKeyConvention>();

            nullConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>(), It.IsAny <Key>()))
            .Returns <InternalKeyBuilder, Key>((b, t) =>
            {
                Assert.Null(t);
                Assert.Same(internalKeyBuilder, b);
                return(false);
            });
            conventions.PrimaryKeySetConventions.Add(nullConvention.Object);

            var extraConvention = new Mock <IPrimaryKeyConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>(), It.IsAny <Key>()))
            .Returns <InternalKeyBuilder, Key>((b, t) =>
            {
                Assert.False(true);
                return(false);
            });
            conventions.PrimaryKeySetConventions.Add(extraConvention.Object);

            var entityBuilder = new InternalModelBuilder(new Model(), conventions)
                                .Entity(typeof(Order), ConfigurationSource.Convention);

            entityBuilder.HasKey(new[] { "OrderId" }, ConfigurationSource.Convention);
            Assert.NotNull(entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention));

            Assert.NotNull(internalKeyBuilder);
        }
Ejemplo n.º 16
0
            public override InternalKeyBuilder OnKeyAdded(InternalKeyBuilder keyBuilder)
            {
                if (keyBuilder.Metadata.Builder == null)
                {
                    return(null);
                }

                foreach (var keyConvention in _conventionSet.KeyAddedConventions)
                {
                    keyBuilder = keyConvention.Apply(keyBuilder);
                    if (keyBuilder?.Metadata.Builder == null)
                    {
                        return(null);
                    }
                }

                return(keyBuilder);
            }
Ejemplo n.º 17
0
        public void OnKeyAdded_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock <IKeyConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.NotNull(b);
                keyBuilder = new InternalKeyBuilder(b.Metadata, b.ModelBuilder);
                return(keyBuilder);
            });
            conventions.KeyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock <IKeyConvention>();

            nullConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.Same(keyBuilder, b);
                return(null);
            });
            conventions.KeyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock <IKeyConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.False(true);
                return(null);
            });
            conventions.KeyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder      = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var explicitKeyBuilder = entityBuilder.Key(new List <string> {
                "OrderId"
            }, ConfigurationSource.Convention);

            Assert.Null(explicitKeyBuilder);
            Assert.NotNull(keyBuilder);
        }
        public void OnKeyRemoved_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock <IKeyRemovedConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <Key>()))
            .Callback <InternalEntityTypeBuilder, Key>((b, k) =>
            {
                Assert.NotNull(b);
                Assert.NotNull(k);
                keyBuilder = new InternalKeyBuilder(k, b.ModelBuilder);
            });
            conventions.KeyRemovedConventions.Add(convention.Object);

            var extraConvention = new Mock <IKeyRemovedConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <Key>()))
            .Callback <InternalEntityTypeBuilder, Key>((b, k) =>
            {
                Assert.NotNull(b);
                Assert.NotNull(k);
                Assert.NotNull(keyBuilder);
            });
            conventions.KeyRemovedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var key           = entityBuilder.HasKey(new List <string> {
                "OrderId"
            }, ConfigurationSource.Convention).Metadata;

            Assert.Same(key, entityBuilder.Metadata.RemoveKey(key.Properties));

            Assert.NotNull(keyBuilder);
        }
Ejemplo n.º 19
0
 public virtual InternalKeyBuilder OnKeyAdded([NotNull] InternalKeyBuilder keyBuilder)
 {
     Add(new OnKeyAddedNode(keyBuilder));
     return(keyBuilder);
 }
Ejemplo n.º 20
0
 public static RelationalKeyBuilderAnnotations Sqlite(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new RelationalKeyBuilderAnnotations(builder, configurationSource, SqliteAnnotationNames.Prefix);
 /// <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 static MySqlKeyBuilderAnnotations MySql(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new MySqlKeyBuilderAnnotations(builder, configurationSource);
Ejemplo n.º 22
0
                public KeyBuilder([NotNull] InternalKeyBuilder builder)
                {
                    Check.NotNull(builder, "builder");

                    _builder = builder;
                }
 public static OracleKeyBuilderAnnotations Oracle(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new OracleKeyBuilderAnnotations(builder, configurationSource);
Ejemplo n.º 24
0
 /// <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 static RelationalKeyBuilderAnnotations Taos(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new RelationalKeyBuilderAnnotations(builder, configurationSource);
 public FbKeyBuilderAnnotations(InternalKeyBuilder internalBuilder, ConfigurationSource configurationSource)
     : base(new RelationalAnnotationsBuilder(internalBuilder, configurationSource))
 {
 }
Ejemplo n.º 26
0
        public KeyBuilder([NotNull] IMutableKey key)
        {
            Check.NotNull(key, nameof(key));

            _builder = ((Key)key).Builder;
        }
Ejemplo n.º 27
0
 public static SqlServerKeyBuilderAnnotations SqlServer(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new SqlServerKeyBuilderAnnotations(builder, configurationSource);
Ejemplo n.º 28
0
 /// <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 SqlServerKeyBuilderAnnotations(
     [NotNull] InternalKeyBuilder internalBuilder,
     ConfigurationSource configurationSource)
     : base(new RelationalAnnotationsBuilder(internalBuilder, configurationSource))
 {
 }
Ejemplo n.º 29
0
        /// <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 InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            SetValueGeneration(keyBuilder.Metadata.Properties);

            return(keyBuilder);
        }
Ejemplo n.º 30
0
 public OnKeyAddedNode(InternalKeyBuilder keyBuilder)
 {
     KeyBuilder = keyBuilder;
 }
Ejemplo n.º 31
0
        public KeyBuilder([NotNull] InternalKeyBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            _builder = builder;
        }
 /// <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 static JetKeyBuilderAnnotations Jet(
     [NotNull] this InternalKeyBuilder builder,
     ConfigurationSource configurationSource)
 => new JetKeyBuilderAnnotations(builder, configurationSource);
Ejemplo n.º 33
0
        public virtual InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            SetValueGeneration(keyBuilder.Metadata.Properties);

            return keyBuilder;
        }
 /// <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 InternalKeyBuilder OnKeyAdded([NotNull] InternalKeyBuilder keyBuilder)
 => _scope.OnKeyAdded(Check.NotNull(keyBuilder, nameof(keyBuilder)));
Ejemplo n.º 35
0
 public static RelationalKeyBuilderAnnotations Firebird(this InternalKeyBuilder builder, ConfigurationSource configurationSource)
 => new RelationalKeyBuilderAnnotations(builder, configurationSource);
        public void OnKeyRemoved_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock<IKeyRemovedConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<Key>()))
                .Callback<InternalEntityTypeBuilder, Key>((b, k) =>
                    {
                        Assert.NotNull(b);
                        Assert.NotNull(k);
                        keyBuilder = new InternalKeyBuilder(k, b.ModelBuilder);
                    });
            conventions.KeyRemovedConventions.Add(convention.Object);

            var extraConvention = new Mock<IKeyRemovedConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<Key>()))
                .Callback<InternalEntityTypeBuilder, Key>((b, k) =>
                    {
                        Assert.NotNull(b);
                        Assert.NotNull(k);
                        Assert.NotNull(keyBuilder);
                    });
            conventions.KeyRemovedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var key = entityBuilder.HasKey(new List<string> { "OrderId" }, ConfigurationSource.Convention).Metadata;

            Assert.Same(key, entityBuilder.Metadata.RemoveKey(key.Properties));

            Assert.NotNull(keyBuilder);
        }
Ejemplo n.º 37
0
 /// <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 static DocumentKeyAnnotations Document([NotNull] this InternalKeyBuilder internalKeyBuilder)
 => Check.NotNull(internalKeyBuilder, nameof(internalKeyBuilder)).Metadata.Document();
Ejemplo n.º 38
0
        /// <inheritdoc />
        public InternalKeyBuilder Apply(InternalKeyBuilder keyBuilder)
        {
            Apply(Check.NotNull(keyBuilder, nameof(keyBuilder)).Metadata.DeclaringEntityType.Builder);

            return(keyBuilder);
        }
        public void OnKeyAdded_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock<IKeyConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b =>
                {
                    Assert.NotNull(b);
                    keyBuilder = new InternalKeyBuilder(b.Metadata, b.ModelBuilder);
                    return keyBuilder;
                });
            conventions.KeyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock<IKeyConvention>();
            nullConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b =>
                {
                    Assert.Same(keyBuilder, b);
                    return null;
                });
            conventions.KeyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock<IKeyConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b =>
                {
                    Assert.False(true);
                    return null;
                });
            conventions.KeyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var explicitKeyBuilder = entityBuilder.HasKey(new List<string> { "OrderId" }, ConfigurationSource.Convention);

            Assert.Null(explicitKeyBuilder);
            Assert.NotNull(keyBuilder);
        }