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

            AddKeyConfiguration(entityConfiguration);
            AddTableNameConfiguration(entityConfiguration);
        }
Exemplo n.º 2
0
        public virtual void AddNavigationPropertyInitializers([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var otherEntityType in entityConfiguration.EntityType.Model
                     .EntityTypes.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 =
                        (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName];
                    if (((EntityType)otherEntityType)
                        .FindAnnotation(RelationalMetadataModelProvider.AnnotationNameEntityTypeError) == null)
                    {
                        if (!foreignKey.IsUnique)
                        {
                            entityConfiguration.NavigationPropertyInitializerConfigurations.Add(
                                new NavigationPropertyInitializerConfiguration(
                                    navigationPropertyName, otherEntityType.Name));
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public PropertyConfiguration(
            [NotNull] EntityConfiguration entityConfiguration, [NotNull] IProperty property)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(property, nameof(property));

            EntityConfiguration = entityConfiguration;
            Property            = property;
        }
Exemplo n.º 4
0
        public virtual void AddEntityPropertiesConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var property in ModelUtilities.OrderedProperties(entityConfiguration.EntityType))
            {
                var propertyConfiguration = new PropertyConfiguration(entityConfiguration, property);
                AddPropertyConfiguration(propertyConfiguration);
                entityConfiguration.PropertyConfigurations.Add(propertyConfiguration);
            }
        }
        public NavigationConfiguration([NotNull] EntityConfiguration entityConfiguration,
                                       [NotNull] IForeignKey foreignKey, [NotNull] string dependentEndNavigationPropertyName,
                                       [NotNull] string principalEndNavigationPropertyName)
        {
            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;
        }
        public override void AddNavigationsConfiguration(EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys())
            {
                var dependentEndNavigationPropertyName =
                    (string)foreignKey[SqlServerMetadataModelProvider.AnnotationNameDependentEndNavPropName];
                var principalEndNavigationPropertyName =
                    (string)foreignKey[SqlServerMetadataModelProvider.AnnotationNamePrincipalEndNavPropName];

                entityConfiguration.RelationshipConfigurations.Add(
                    new RelationshipConfiguration(entityConfiguration, foreignKey,
                        dependentEndNavigationPropertyName, principalEndNavigationPropertyName));
            }
        }
        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.º 8
0
        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 != DefaultSchemaName)
            {
                var delimitedTableName =
                    CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).TableName);
                var delimitedSchemaName =
                    CSharpUtilities.DelimitString(ExtensionsProvider.For(entityType).Schema);
                entityConfiguration.FluentApiConfigurations.Add(
                    new FluentApiConfiguration(
                        nameof(RelationalEntityTypeBuilderExtensions.ToTable),
                        delimitedTableName,
                        delimitedSchemaName)
                {
                    HasAttributeEquivalent = true
                });
                entityConfiguration.AttributeConfigurations.Add(
                    new AttributeConfiguration(
                        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(
                    new FluentApiConfiguration(
                        nameof(RelationalEntityTypeBuilderExtensions.ToTable),
                        delimitedTableName)
                {
                    HasAttributeEquivalent = true
                });
                entityConfiguration.AttributeConfigurations.Add(
                    new AttributeConfiguration(
                        nameof(TableAttribute), delimitedTableName));
            }
        }
Exemplo n.º 9
0
        public virtual void AddRelationshipConfiguration([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var foreignKey in entityConfiguration.EntityType.GetForeignKeys())
            {
                var dependentEndNavigationPropertyName =
                    (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName];
                var principalEndNavigationPropertyName =
                    (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName];

                entityConfiguration.RelationshipConfigurations.Add(
                    new RelationshipConfiguration(entityConfiguration, foreignKey,
                                                  dependentEndNavigationPropertyName, principalEndNavigationPropertyName)
                {
                    HasAttributeEquivalent = foreignKey.PrincipalKey.IsPrimaryKey()
                });
            }
        }
Exemplo n.º 10
0
        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);
                if (conventionKeyProperties != null &&
                    key.Properties.OrderBy(p => p.Name).SequenceEqual(conventionKeyProperties.OrderBy(p => p.Name)))
                {
                    continue;
                }

                var keyFluentApi = new KeyFluentApiConfiguration("e", key.Properties);

                if (key.IsPrimaryKey())
                {
                    keyFluentApi.IsPrimaryKey = true;

                    if (key.Properties.Count == 1)
                    {
                        keyFluentApi.HasAttributeEquivalent = true;

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

                entityConfiguration.FluentApiConfigurations.Add(keyFluentApi);
            }
        }
Exemplo n.º 11
0
        public virtual void AddEntityConfigurations()
        {
            var entityConfigurations = new List <EntityConfiguration>();

            foreach (var entityType in Model.EntityTypes)
            {
                var entityConfiguration = new EntityConfiguration(this, entityType);
                if (entityConfiguration.ErrorMessageAnnotation == null)
                {
                    AddEntityPropertiesConfiguration(entityConfiguration);
                    AddEntityConfiguration(entityConfiguration);
                    AddNavigationProperties(entityConfiguration);
                    AddNavigationPropertyInitializers(entityConfiguration);
                    AddRelationshipConfiguration(entityConfiguration);
                }

                _entityConfigurationMap.Add((EntityType)entityType, entityConfiguration);
            }
        }
Exemplo n.º 12
0
        public virtual void AddNavigationProperties([NotNull] EntityConfiguration entityConfiguration)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));

            foreach (var otherEntityType in entityConfiguration.EntityType
                     .Model.EntityTypes.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))
                {
                    if (((EntityType)otherEntityType)
                        .FindAnnotation(RelationalMetadataModelProvider.AnnotationNameEntityTypeError) != null)
                    {
                        entityConfiguration.NavigationPropertyConfigurations.Add(
                            new NavigationPropertyConfiguration(
                                RelationalDesignStrings.UnableToAddNavigationProperty(otherEntityType.Name)));
                    }
                    else
                    {
                        var referencedType = foreignKey.IsUnique
                            ? otherEntityType.Name
                            : "ICollection<" + otherEntityType.Name + ">";
                        var navPropConfiguration =
                            new NavigationPropertyConfiguration(
                                referencedType,
                                (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName]);

                        if (foreignKey.PrincipalKey.IsPrimaryKey())
                        {
                            navPropConfiguration.AttributeConfigurations.Add(
                                new AttributeConfiguration(
                                    nameof(InversePropertyAttribute),
                                    CSharpUtilities.DelimitString(
                                        (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName])));
                        }

                        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 =
                    new NavigationPropertyConfiguration(
                        foreignKey.PrincipalEntityType.Name,
                        (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName]);

                if (foreignKey.PrincipalKey.IsPrimaryKey())
                {
                    dependentEndNavPropConfiguration.AttributeConfigurations.Add(
                        new AttributeConfiguration(
                            nameof(ForeignKeyAttribute),
                            CSharpUtilities.DelimitString(
                                string.Join(",", foreignKey.Properties.Select(p => p.Name)))));
                    dependentEndNavPropConfiguration.AttributeConfigurations.Add(
                        new AttributeConfiguration(
                            nameof(InversePropertyAttribute),
                            CSharpUtilities.DelimitString(
                                (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName])));
                }

                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 = new NavigationPropertyConfiguration(
                        referencedType,
                        (string)foreignKey[RelationalMetadataModelProvider.AnnotationNamePrincipalEndNavPropName]);
                    principalEndNavPropConfiguration.AttributeConfigurations.Add(
                        new AttributeConfiguration(
                            nameof(InversePropertyAttribute),
                            CSharpUtilities.DelimitString(
                                (string)foreignKey[RelationalMetadataModelProvider.AnnotationNameDependentEndNavPropName])));
                    entityConfiguration.NavigationPropertyConfigurations.Add(
                        principalEndNavPropConfiguration);
                }
            }
        }
Exemplo n.º 13
0
        public virtual void AddEntityConfigurations()
        {
            var entityConfigurations = new List<EntityConfiguration>();

            foreach (var entityType in Model.EntityTypes)
            {
                var entityConfiguration = new EntityConfiguration(this, entityType);
                if (entityConfiguration.ErrorMessageAnnotation == null)
                {
                    AddEntityPropertiesConfiguration(entityConfiguration);
                    AddEntityConfiguration(entityConfiguration);
                    AddNavigationProperties(entityConfiguration);
                    AddNavigationPropertyInitializers(entityConfiguration);
                    AddRelationshipConfiguration(entityConfiguration);
                }

                _entityConfigurationMap.Add((EntityType)entityType, entityConfiguration);
            }
        }