Exemplo n.º 1
0
        public virtual string WriteCode(
            [NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            _entity = entityConfiguration;
            _sb = new IndentedStringBuilder();

            _sb.AppendLine("using System;");
            _sb.AppendLine("using System.Collections.Generic;");
            if (!_entity.ModelConfiguration.CustomConfiguration.UseFluentApiOnly)
            {
                _sb.AppendLine("using System.ComponentModel.DataAnnotations;");
                _sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
            }

            _sb.AppendLine();
            _sb.AppendLine("namespace " + _entity.ModelConfiguration.Namespace());
            _sb.AppendLine("{");
            using (_sb.Indent())
            {
                AddClass();
            }
            _sb.AppendLine("}");

            return _sb.ToString();
        }
        public virtual void AddEntityConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            AddKeyConfiguration(entityConfiguration);
            AddTableNameConfiguration(entityConfiguration);
            AddIndexConfigurations(entityConfiguration);
        }
        public PropertyConfiguration(
            [NotNull] EntityConfiguration entityConfiguration, [NotNull] IProperty property)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(property, nameof(property));

            EntityConfiguration = entityConfiguration;
            Property            = property;
        }
        public virtual void AddIndexConfigurations([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var index in entityConfiguration.EntityType.GetIndexes().Cast <Index>())
            {
                AddIndexConfiguration(entityConfiguration, index);
            }
        }
        public virtual void AddIndexConfiguration(
            [NotNull] EntityConfiguration entityConfiguration,
            [NotNull] Index index)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(index, nameof(index));

            entityConfiguration.FluentApiConfigurations.Add(
                _configurationFactory.CreateIndexConfiguration("e", index));
        }
        public virtual void AddEntityPropertiesConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var property in ScaffoldingUtilities.OrderedProperties(entityConfiguration.EntityType))
            {
                var propertyConfiguration =
                    _configurationFactory.CreatePropertyConfiguration(entityConfiguration, property);
                AddPropertyConfiguration(propertyConfiguration);
                entityConfiguration.PropertyConfigurations.Add(propertyConfiguration);
            }
        }
        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.Name)))
                    {
                        keyFluentApi.HasAttributeEquivalent = true;

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

                    entityConfiguration.FluentApiConfigurations.Add(keyFluentApi);
                }
            }
        }
Exemplo n.º 8
0
        public virtual PropertyConfiguration GetOrAddPropertyConfiguration(
            [NotNull] EntityConfiguration entityConfiguration, [NotNull] Property property)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(property, nameof(property));

            var propertyConfiguration = FindPropertyConfiguration(property);

            if (propertyConfiguration == null)
            {
                propertyConfiguration = new PropertyConfiguration(entityConfiguration, property);
                PropertyConfigurations.Add(propertyConfiguration);
            }

            return(propertyConfiguration);
        }
Exemplo n.º 9
0
        public virtual void AddIndexConfigurations([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            var entityType           = (EntityType)entityConfiguration.EntityType;
            var primaryKeyProperties = entityType.FindPrimaryKey()?.Properties;

            foreach (var index in entityType.GetIndexes())
            {
                // do not add indexes for the primary key
                if (!index.Properties.SequenceEqual(primaryKeyProperties))
                {
                    AddIndexConfiguration(entityConfiguration, index);
                }
            }
        }
Exemplo n.º 10
0
        public RelationshipConfiguration(
            [NotNull] EntityConfiguration entityConfiguration,
            [NotNull] IForeignKey foreignKey,
            [NotNull] string dependentEndNavigationPropertyName,
            [NotNull] string principalEndNavigationPropertyName,
            DeleteBehavior onDeleteAction)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(foreignKey, nameof(foreignKey));
            Check.NotEmpty(dependentEndNavigationPropertyName, nameof(dependentEndNavigationPropertyName));
            Check.NotEmpty(principalEndNavigationPropertyName, nameof(principalEndNavigationPropertyName));

            EntityConfiguration = entityConfiguration;
            ForeignKey          = foreignKey;
            DependentEndNavigationPropertyName = dependentEndNavigationPropertyName;
            PrincipalEndNavigationPropertyName = principalEndNavigationPropertyName;
            OnDeleteAction = onDeleteAction;
        }
        public virtual void AddNavigationPropertyInitializers([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var otherEntityType in entityConfiguration.EntityType.Model.GetEntityTypes().Where(et => et != entityConfiguration.EntityType))
            {
                // find navigation properties for foreign keys from another EntityType which reference this EntityType
                foreach (var foreignKey in otherEntityType
                         .GetForeignKeys().Where(fk => fk.PrincipalEntityType == entityConfiguration.EntityType))
                {
                    var navigationPropertyName =
                        foreignKey.Scaffolding().PrincipalEndNavigation;
                    if (!foreignKey.IsUnique)
                    {
                        entityConfiguration.NavigationPropertyInitializerConfigurations.Add(
                            _configurationFactory.CreateNavigationPropertyInitializerConfiguration(
                                navigationPropertyName, otherEntityType.Name));
                    }
                }
            }
        }
        public virtual void AddTableNameConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            var entityType = entityConfiguration.EntityType;

            if (ExtensionsProvider.For(entityType).Schema != null &&
                ExtensionsProvider.For(entityType).Schema != ExtensionsProvider.For(Model).DefaultSchema)
            {
                var delimitedTableName =
                    CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).TableName);
                var delimitedSchemaName =
                    CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).Schema);
                entityConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ true,
                        nameof(RelationalEntityTypeBuilderExtensions.ToTable),
                        delimitedTableName,
                        delimitedSchemaName));
                entityConfiguration.AttributeConfigurations.Add(
                    _configurationFactory.CreateAttributeConfiguration(
                        nameof(TableAttribute),
                        delimitedTableName,
                        nameof(TableAttribute.Schema) + " = " + delimitedSchemaName));
            }
            else if (ExtensionsProvider.For(entityType).TableName != null &&
                     ExtensionsProvider.For(entityType).TableName != entityType.DisplayName())
            {
                var delimitedTableName =
                    CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).TableName);
                entityConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ true,
                        nameof(RelationalEntityTypeBuilderExtensions.ToTable),
                        delimitedTableName));
                entityConfiguration.AttributeConfigurations.Add(
                    _configurationFactory.CreateAttributeConfiguration(
                        nameof(TableAttribute), delimitedTableName));
            }
        }
        public virtual void AddRelationshipConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys())
            {
                var dependentEndNavigationPropertyName =
                    foreignKey.Scaffolding().DependentEndNavigation;
                var principalEndNavigationPropertyName =
                    foreignKey.Scaffolding().PrincipalEndNavigation;

                var relationshipConfiguration = _configurationFactory
                                                .CreateRelationshipConfiguration(
                    entityConfiguration,
                    foreignKey,
                    dependentEndNavigationPropertyName,
                    principalEndNavigationPropertyName,
                    foreignKey.DeleteBehavior);
                relationshipConfiguration.HasAttributeEquivalent = foreignKey.PrincipalKey.IsPrimaryKey();
                entityConfiguration.RelationshipConfigurations.Add(relationshipConfiguration);
            }
        }
        public virtual void AddNavigationProperties([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var otherEntityType in entityConfiguration.EntityType
                     .Model.GetEntityTypes().Where(et => et != entityConfiguration.EntityType))
            {
                // set up the navigation properties for foreign keys from another EntityType
                // which reference this EntityType (i.e. this EntityType is the principal)
                foreach (var foreignKey in otherEntityType
                         .GetForeignKeys().Where(fk => fk.PrincipalEntityType == entityConfiguration.EntityType))
                {
                    var referencedType = foreignKey.IsUnique
                        ? otherEntityType.Name
                        : "ICollection<" + otherEntityType.Name + ">";
                    var navPropConfiguration =
                        _configurationFactory.CreateNavigationPropertyConfiguration(
                            referencedType,
                            foreignKey.Scaffolding().PrincipalEndNavigation);

                    if (foreignKey.PrincipalKey.IsPrimaryKey())
                    {
                        navPropConfiguration.AttributeConfigurations.Add(
                            _configurationFactory.CreateAttributeConfiguration(
                                nameof(InversePropertyAttribute),
                                CSharpUtilities.DelimitString(
                                    foreignKey.Scaffolding().DependentEndNavigation)));
                    }

                    entityConfiguration.NavigationPropertyConfigurations.Add(navPropConfiguration);
                }
            }

            foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys())
            {
                // set up the navigation property on this end of foreign keys owned by this EntityType
                // (i.e. this EntityType is the dependent)
                var dependentEndNavPropConfiguration =
                    _configurationFactory.CreateNavigationPropertyConfiguration(
                        foreignKey.PrincipalEntityType.Name,
                        foreignKey.Scaffolding().DependentEndNavigation);

                if (foreignKey.PrincipalKey.IsPrimaryKey())
                {
                    dependentEndNavPropConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(
                            nameof(ForeignKeyAttribute),
                            CSharpUtilities.DelimitString(
                                string.Join(",", foreignKey.Properties.Select(p => p.Name)))));
                    dependentEndNavPropConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(
                            nameof(InversePropertyAttribute),
                            CSharpUtilities.DelimitString(
                                foreignKey.Scaffolding().PrincipalEndNavigation)));
                }

                entityConfiguration.NavigationPropertyConfigurations.Add(
                    dependentEndNavPropConfiguration);

                // set up the other navigation property for self-referencing foreign keys owned by this EntityType
                if (((ForeignKey)foreignKey).IsSelfReferencing())
                {
                    var referencedType = foreignKey.IsUnique
                        ? foreignKey.DeclaringEntityType.Name
                        : "ICollection<" + foreignKey.DeclaringEntityType.Name + ">";
                    var principalEndNavPropConfiguration =
                        _configurationFactory.CreateNavigationPropertyConfiguration(
                            referencedType,
                            foreignKey.Scaffolding().PrincipalEndNavigation);
                    principalEndNavPropConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(
                            nameof(InversePropertyAttribute),
                            CSharpUtilities.DelimitString(
                                foreignKey.Scaffolding().DependentEndNavigation)));
                    entityConfiguration.NavigationPropertyConfigurations.Add(
                        principalEndNavPropConfiguration);
                }
            }
        }