Exemple #1
0
        public override void AddTableNameConfiguration(EntityConfiguration entityConfiguration)
        {
            // Rather than being smart, we're just always configuring the
            // table name for every entity.

            var entityType          = entityConfiguration.EntityType;
            var delimitedTableName  = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).TableName);
            var delimitedSchemaName = CSharpUtilities.DelimitString(AnnotationProvider.For(entityType).Schema);

            entityConfiguration.FluentApiConfigurations.Add(
                _configurationFactory.CreateFluentApiConfiguration(
                    true, /* <= hasAttributeEquivalent */
                    nameof(RelationalEntityTypeBuilderExtensions.ToTable),
                    delimitedTableName,
                    delimitedSchemaName));

            entityConfiguration.AttributeConfigurations.Add(
                _configurationFactory.CreateAttributeConfiguration(
                    nameof(TableAttribute),
                    delimitedTableName,
                    $"{nameof(TableAttribute.Schema)} = {delimitedSchemaName}"));
        }
Exemple #2
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 void AddKeyConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            var entityType = (EntityType)entityConfiguration.EntityType;

            foreach (var key in entityType.GetKeys())
            {
                if (key == null ||
                    key.Properties.Count == 0)
                {
                    continue;
                }

                var conventionKeyProperties =
                    _keyDiscoveryConvention.DiscoverKeyProperties(entityType, entityType.GetProperties().ToList());
                if (conventionKeyProperties != null &&
                    key.Properties.OrderBy(p => p.Name).SequenceEqual(conventionKeyProperties.OrderBy(p => p.Name)))
                {
                    continue;
                }

                if (key.IsPrimaryKey())
                {
                    var keyFluentApi = _configurationFactory
                                       .CreateKeyFluentApiConfiguration("e", key);

                    if (key.Properties.Count == 1 &&
                        key.Relational().Name ==
                        RelationalKeyAnnotations
                        .GetDefaultKeyName(
                            entityType.Relational().TableName,
                            true,     /* is primary key */
                            key.Properties.Select(p => p.Relational().ColumnName)))
                    {
                        keyFluentApi.HasAttributeEquivalent = true;

                        var propertyConfiguration =
                            entityConfiguration.GetOrAddPropertyConfiguration(
                                entityConfiguration, key.Properties.First());
                        propertyConfiguration.AttributeConfigurations.Add(
                            _configurationFactory.CreateAttributeConfiguration(nameof(KeyAttribute)));
                    }

                    entityConfiguration.FluentApiConfigurations.Add(keyFluentApi);
                }
            }
        }