Esempio n. 1
0
        private static void SetupMappings(MappingSet set)
        {
            MapAllColumns(set, set.Database.Tables[0], set.EntitySet.Entities[0]);
            MapAllColumns(set, set.Database.Tables[1], set.EntitySet.Entities[1]);

            set.ChangeMappingFor(set.EntitySet.Entities[0].References[0])
                .To(set.Database.Tables[0].Relationships[0]);
        }
Esempio n. 2
0
        private static void SetupMappings(MappingSet set)
        {
            MapAllColumns(set, set.Database.Tables[0], set.EntitySet.Entities[0]);
            MapAllColumns(set, set.Database.Tables[1], set.EntitySet.Entities[1]);

            set.ChangeMappingFor(set.EntitySet.Entities[0].References[0])
            .To(set.Database.Tables[0].Relationships[0]);
        }
Esempio n. 3
0
        public void All_Reference_Mappings_Are_Deleted()
        {
            var reference = entitySet.Entities[0].CreateReferenceTo(entitySet.Entities[1]);

            mappingSet.ChangeMappingFor(reference).To(table1);

            Assert.That(reference.MappedTable(), Is.EqualTo(table1));

            table1.DeleteSelf();

            Assert.That(reference.MappedTable(), Is.Null);
        }
        public void Setup()
        {
            set = new MappingSetImpl();
            entity = new EntityImpl("Entity1");
            set.EntitySet.AddEntity(entity);
            table = new Table("Table1");
            table.AddColumn(new Column("Street"));
            set.Database.AddTable(table);

            spec = new ComponentSpecificationImpl("Address");
            spec.AddProperty(new ComponentPropertyImpl("Street"));
            set.EntitySet.AddComponentSpecification(spec);

            component = spec.CreateImplementedComponentFor(entity, "HomeAddress");
            set.ChangeMappingFor(component.Properties[0]).To(table.Columns[0]);
        }
        public void Setup()
        {
            set    = new MappingSetImpl();
            entity = new EntityImpl("Entity1");
            set.EntitySet.AddEntity(entity);
            table = new Table("Table1");
            table.AddColumn(new Column("Street"));
            set.Database.AddTable(table);

            spec = new ComponentSpecificationImpl("Address");
            spec.AddProperty(new ComponentPropertyImpl("Street"));
            set.EntitySet.AddComponentSpecification(spec);

            component = spec.CreateImplementedComponentFor(entity, "HomeAddress");
            set.ChangeMappingFor(component.Properties[0]).To(table.Columns[0]);
        }
Esempio n. 6
0
        private static void ProcessOneToOneReference(MappingSet set, Reference reference)
        {
            var fromTable = reference.Entity1.MappedTables().First();
            var toTable   = reference.Entity2.MappedTables().First();

            // Create a foreign key on the to table
            Key foreignKey = CreateForeignKey(set, fromTable, toTable);

            // Create a Unique key from the foreign key we just created. This constrains the
            // relationship between the tables to One to One.
            CreateUniqueKeyFor(foreignKey);

            // Create the relationship between the two.
            var relationship = fromTable.CreateRelationshipUsing(fromTable.FirstPrimaryKey, foreignKey);

            set.ChangeMappingFor(reference).To(relationship);
        }
Esempio n. 7
0
        private static void ProcessOneToManyReference(MappingSet set, Reference reference)
        {
            var manyEnd     = reference.Cardinality1 == Cardinality.Many ? reference.Entity1 : reference.Entity2;
            var directedRef = new DirectedReference(manyEnd, reference);

            var oneEnd       = directedRef.ToEntity;
            var oneEndTable  = oneEnd.MappedTables().First();
            var manyEndTable = manyEnd.MappedTables().First();

            IKey primaryKey = oneEndTable.FirstPrimaryKey;
            // Create a foreign key on the Many side
            Key foreignKey = CreateForeignKey(set, oneEndTable, manyEndTable);

            // Create the relationship between the two.
            var relationship = oneEndTable.CreateRelationshipUsing(primaryKey, foreignKey);

            set.ChangeMappingFor(reference).To(relationship);
        }
Esempio n. 8
0
        private static void ProcessManyToManyReference(MappingSet set, Reference reference)
        {
            var table1 = reference.Entity1.MappedTables().First();
            var table2 = reference.Entity2.MappedTables().First();

            // Create association table
            var associationTable = new Table(table1.Name + "_" + table2.Name + "_AssociationTable", table1.Schema);

            set.Database.AddTable(associationTable);
            set.ChangeMappingFor(reference).To(associationTable);

            // Create foreign keys (and columns) on the association table.
            var foreignKey1 = CreateForeignKey(set, table1, associationTable);
            var foreignKey2 = CreateForeignKey(set, table2, associationTable);

            // Create Relationships to the association table.
            table1.CreateRelationshipUsing(table1.FirstPrimaryKey, foreignKey1);
            table2.CreateRelationshipUsing(table2.FirstPrimaryKey, foreignKey2);
        }
        public void ApplyChanges(MappingSet mappingSet, EntityKey key)
        {
            // Create the new Spec
            ComponentSpecification spec = new ComponentSpecificationImpl(newComponentSpecificationName);

            var properties = key.Properties.ToList();

            foreach (var property in properties)
            {
                var newProperty = ComponentPropertyImpl.CreateFromProperty(property);
                spec.AddProperty(newProperty);
            }

            mappingSet.EntitySet.AddComponentSpecification(spec);

            // Create the new Component
            var component = spec.CreateImplementedComponentFor(key.Parent, newComponentName);

            // Map the old property's columns to the new component
            for (int i = 0; i < properties.Count; i++)
            {
                var property    = properties[i];
                var newProperty = spec.Properties[i];

                var componentProperty = component.GetProperty(newProperty.Name);
                mappingSet.ChangeMappingFor(componentProperty).To(property.MappedColumn());
            }

            // Delete old properties
            if (deleteExistingProperties)
            {
                foreach (var property in properties)
                {
                    property.DeleteSelf();
                }
            }

            // Set the Key to use the new component.
            key.Component = component;
        }
        public void ApplyChanges(MappingSet mappingSet, EntityKey key)
        {
            // Create the new Spec
            ComponentSpecification spec = new ComponentSpecificationImpl(newComponentSpecificationName);

            var properties = key.Properties.ToList();
            foreach (var property in properties)
            {
                var newProperty = ComponentPropertyImpl.CreateFromProperty(property);
                spec.AddProperty(newProperty);
            }

            mappingSet.EntitySet.AddComponentSpecification(spec);

            // Create the new Component
            var component = spec.CreateImplementedComponentFor(key.Parent, newComponentName);

            // Map the old property's columns to the new component
            for (int i = 0; i < properties.Count; i++)
            {
                var property = properties[i];
                var newProperty = spec.Properties[i];

                var componentProperty = component.GetProperty(newProperty.Name);
                mappingSet.ChangeMappingFor(componentProperty).To(property.MappedColumn());
            }

            // Delete old properties
            if(deleteExistingProperties)
            {
                foreach(var property in properties)
                {
                    property.DeleteSelf();
                }
            }

            // Set the Key to use the new component.
            key.Component = component;
        }
Esempio n. 11
0
        private static void ProcessOneToOneReference(MappingSet set, Reference reference)
        {
            var fromTable = reference.Entity1.MappedTables().First();
            var toTable = reference.Entity2.MappedTables().First();

            // Create a foreign key on the to table
            Key foreignKey = CreateForeignKey(set, fromTable, toTable);

            // Create a Unique key from the foreign key we just created. This constrains the
            // relationship between the tables to One to One.
            CreateUniqueKeyFor(foreignKey);

            // Create the relationship between the two.
            var relationship = fromTable.CreateRelationshipUsing(fromTable.FirstPrimaryKey, foreignKey);
            set.ChangeMappingFor(reference).To(relationship);
        }
Esempio n. 12
0
        private static void ProcessOneToManyReference(MappingSet set, Reference reference)
        {
            var manyEnd = reference.Cardinality1 == Cardinality.Many ? reference.Entity1 : reference.Entity2;
            var directedRef = new DirectedReference(manyEnd, reference);

            var oneEnd = directedRef.ToEntity;
            var oneEndTable = oneEnd.MappedTables().First();
            var manyEndTable = manyEnd.MappedTables().First();

            IKey primaryKey = oneEndTable.FirstPrimaryKey;
            // Create a foreign key on the Many side
            Key foreignKey = CreateForeignKey(set, oneEndTable, manyEndTable);

            // Create the relationship between the two.
            var relationship = oneEndTable.CreateRelationshipUsing(primaryKey, foreignKey);

            set.ChangeMappingFor(reference).To(relationship);
        }
Esempio n. 13
0
        private static void ProcessManyToManyReference(MappingSet set, Reference reference)
        {
            var table1 = reference.Entity1.MappedTables().First();
            var table2 = reference.Entity2.MappedTables().First();

            // Create association table
            var associationTable = new Table(table1.Name + "_" + table2.Name + "_AssociationTable", table1.Schema);
            set.Database.AddTable(associationTable);
            set.ChangeMappingFor(reference).To(associationTable);

            // Create foreign keys (and columns) on the association table.
            var foreignKey1 = CreateForeignKey(set, table1, associationTable);
            var foreignKey2 = CreateForeignKey(set, table2, associationTable);

            // Create Relationships to the association table.
            table1.CreateRelationshipUsing(table1.FirstPrimaryKey, foreignKey1);
            table2.CreateRelationshipUsing(table2.FirstPrimaryKey, foreignKey2);
        }
Esempio n. 14
0
        private void UseDiscoveredInformationToCreateReferences(MappingSet mappingSet)
        {
            while (associationInformation.Count > 0)
            {
                var info = associationInformation[0];
                associationInformation.RemoveAt(0);
                // Get existing objects
                var fromEntity = mappingSet.EntitySet.GetEntity(info.ThisEntityName);
                var fromTable  = fromEntity.MappedTables().First();
                var toEntity   = mappingSet.EntitySet.GetEntity(info.OtherEntityName);
                var toTable    = toEntity.MappedTables().First();

                // Sort out this side of the reference.
                Reference reference = new ReferenceImpl();
                reference.Entity1      = fromEntity;
                reference.End1Name     = info.PropertyName;
                reference.End1Enabled  = true;
                reference.Cardinality1 = info.Cardinality;
                reference.SetEnd1AssociationType(info.AssociationType);
                reference.SetEnd1IndexColumnName(info.IndexColumn);
                reference.SetEnd1SqlWhereClause(info.WhereClause);
                reference.SetReferenceEnd1FetchMode(info.FetchMode);
                reference.SetReferenceEnd1CollectionFetchMode(info.CollectionFetchMode);
                reference.SetReferenceEnd1Insert(info.Insert);
                reference.SetReferenceEnd1Update(info.Update);
                reference.SetReferenceEnd1Inverse(info.Inverse);
                reference.SetReferenceEnd1FetchMode(info.FetchMode);
                reference.SetReferenceEnd1Lazy(info.CollectionLazy);
                reference.SetReferenceEnd1Cascade(info.Cascade);
                reference.SetReferenceEnd1CollectionCascade(info.CollectionCascade);

                if (!string.IsNullOrWhiteSpace(info.OrderByColumnName))
                {
                    var orderByProp = fromEntity.Properties.SingleOrDefault(p => p.MappedColumn() != null && p.MappedColumn().Name.Equals(info.OrderByColumnName, StringComparison.InvariantCultureIgnoreCase));

                    if (orderByProp != null)
                    {
                        reference.SetReferenceEnd1OrderByProperty(orderByProp.Name);
                    }
                }
                reference.SetReferenceEnd1OrderByIsAsc(info.OrderByIsAsc);
                // Find the other side of the reference.
                ProcessOtherEndOfReference(info, toEntity, reference);
                //associationInformation.RemoveAt(0);
                fromEntity.AddReference(reference);

                if (fromEntity.InternalIdentifier != toEntity.InternalIdentifier)
                {
                    toEntity.AddReference(reference);
                }

                if (info.AssociationTableName != null &&
                    !string.IsNullOrEmpty(info.AssociationTableName.TableName))
                {
                    // Map Reference to Table
                    string schema      = /*string.IsNullOrEmpty(info.AssociationTableName.SchemaName) ? "" :*/ info.AssociationTableName.SchemaName.UnBackTick();
                    var    mappedTable = mappingSet.Database.GetTable(info.AssociationTableName.TableName.UnBackTick(), schema);

                    if (mappedTable == null)
                    {
                        throw new NHibernateMappingException(string.Format("Could not find association table {0} to map to reference {1}.", info.AssociationTableName, reference.Name));
                    }

                    mappingSet.ChangeMappingFor(reference).To(mappedTable);
                }
                else
                {
                    IEnumerable <IColumn>             foreignKeyColumns;
                    Func <DirectedRelationship, bool> predicate;

                    if (info.ForeignKeyColumnNames.Count == 0 && info.Cardinality.Start == 1 && info.Cardinality.End == 1 && fromTable != toTable)
                    {
                        predicate = r =>
                                    r.ToKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.ToTable.ColumnsInPrimaryKey.OrderBy(c => c.Name)) &&
                                    r.FromKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.FromTable.ColumnsInPrimaryKey.OrderBy(c => c.Name));
                    }
                    else
                    {
                        if (info.ForeignKeyBelongsToThisTable)
                        {
                            foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => fromTable.GetColumn(f.UnBackTick()));

                            if (fromTable == toTable)
                            {
                                // Self referencing keys might have the primary key at either end.
                                predicate = r => (r.FromKey.Columns.SequenceEqual(foreignKeyColumns) || r.ToKey.Columns.SequenceEqual(foreignKeyColumns));
                            }
                            else
                            {
                                predicate = r => r.FromKey.Columns.SequenceEqual(foreignKeyColumns);
                            }
                        }
                        else
                        {
                            foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => toTable.GetColumn(f.UnBackTick()));
                            predicate         = r => r.ToKey.Columns.SequenceEqual(foreignKeyColumns);
                        }
                    }
                    var possibleRelationships = fromTable.DirectedRelationships.Where(r => r.ToTable == toTable);
                    var relationshipToMap     = possibleRelationships.FirstOrDefault(predicate);

                    if (relationshipToMap != null)
                    {
                        mappingSet.ChangeMappingFor(reference).To(relationshipToMap.Relationship);
                    }
                    else
                    {
                        throw new NHibernateMappingException(string.Format("Could not find relationship to map to for reference between Entities \"{0}\" and \"{1}\"", info.ThisEntityName, info.OtherEntityName));
                    }
                }
            }
        }
Esempio n. 15
0
        private void UseDiscoveredInformationToCreateReferences(MappingSet mappingSet)
        {
            while (associationInformation.Count > 0)
            {
                var info = associationInformation[0];
                associationInformation.RemoveAt(0);
                // Get existing objects
                var fromEntity = mappingSet.EntitySet.GetEntity(info.ThisEntityName);
                var fromTable = fromEntity.MappedTables().First();
                var toEntity = mappingSet.EntitySet.GetEntity(info.OtherEntityName);
                var toTable = toEntity.MappedTables().First();

                // Sort out this side of the reference.
                Reference reference = new ReferenceImpl();
                reference.Entity1 = fromEntity;
                reference.End1Name = info.PropertyName;
                reference.End1Enabled = true;
                reference.Cardinality1 = info.Cardinality;
                reference.SetEnd1AssociationType(info.AssociationType);
                reference.SetEnd1IndexColumnName(info.IndexColumn);
                reference.SetEnd1SqlWhereClause(info.WhereClause);
                reference.SetReferenceEnd1FetchMode(info.FetchMode);
                reference.SetReferenceEnd1CollectionFetchMode(info.CollectionFetchMode);
                reference.SetReferenceEnd1Insert(info.Insert);
                reference.SetReferenceEnd1Update(info.Update);
                reference.SetReferenceEnd1Inverse(info.Inverse);
                reference.SetReferenceEnd1FetchMode(info.FetchMode);
                reference.SetReferenceEnd1Lazy(info.CollectionLazy);
                reference.SetReferenceEnd1Cascade(info.Cascade);
                reference.SetReferenceEnd1CollectionCascade(info.CollectionCascade);

                if (!string.IsNullOrWhiteSpace(info.OrderByColumnName))
                {
                    var orderByProp = fromEntity.Properties.SingleOrDefault(p => p.MappedColumn() != null && p.MappedColumn().Name.Equals(info.OrderByColumnName, StringComparison.InvariantCultureIgnoreCase));

                    if (orderByProp != null)
                        reference.SetReferenceEnd1OrderByProperty(orderByProp.Name);
                }
                reference.SetReferenceEnd1OrderByIsAsc(info.OrderByIsAsc);
                // Find the other side of the reference.
                ProcessOtherEndOfReference(info, toEntity, reference);
                //associationInformation.RemoveAt(0);
                fromEntity.AddReference(reference);

                if (fromEntity.InternalIdentifier != toEntity.InternalIdentifier)
                    toEntity.AddReference(reference);

                if (info.AssociationTableName != null &&
                    !string.IsNullOrEmpty(info.AssociationTableName.TableName))
                {
                    // Map Reference to Table
                    string schema = /*string.IsNullOrEmpty(info.AssociationTableName.SchemaName) ? "" :*/ info.AssociationTableName.SchemaName.UnBackTick();
                    var mappedTable = mappingSet.Database.GetTable(info.AssociationTableName.TableName.UnBackTick(), schema);

                    if (mappedTable == null)
                        throw new NHibernateMappingException(string.Format("Could not find association table {0} to map to reference {1}.", info.AssociationTableName, reference.Name));

                    mappingSet.ChangeMappingFor(reference).To(mappedTable);
                }
                else
                {
                    IEnumerable<IColumn> foreignKeyColumns;
                    Func<DirectedRelationship, bool> predicate;

                    if (info.ForeignKeyColumnNames.Count == 0 && info.Cardinality.Start == 1 && info.Cardinality.End == 1 && fromTable != toTable)
                    {
                        predicate = r =>
                            r.ToKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.ToTable.ColumnsInPrimaryKey.OrderBy(c => c.Name)) &&
                            r.FromKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.FromTable.ColumnsInPrimaryKey.OrderBy(c => c.Name));
                    }
                    else
                    {
                        if (info.ForeignKeyBelongsToThisTable)
                        {
                            foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => fromTable.GetColumn(f.UnBackTick()));

                            if (fromTable == toTable)
                            {
                                // Self referencing keys might have the primary key at either end.
                                predicate = r => (r.FromKey.Columns.SequenceEqual(foreignKeyColumns) || r.ToKey.Columns.SequenceEqual(foreignKeyColumns));
                            }
                            else
                                predicate = r => r.FromKey.Columns.SequenceEqual(foreignKeyColumns);
                        }
                        else
                        {
                            foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => toTable.GetColumn(f.UnBackTick()));
                            predicate = r => r.ToKey.Columns.SequenceEqual(foreignKeyColumns);
                        }
                    }
                    var possibleRelationships = fromTable.DirectedRelationships.Where(r => r.ToTable == toTable);
                    var relationshipToMap = possibleRelationships.FirstOrDefault(predicate);

                    if (relationshipToMap != null)
                        mappingSet.ChangeMappingFor(reference).To(relationshipToMap.Relationship);
                    else
                        throw new NHibernateMappingException(string.Format("Could not find relationship to map to for reference between Entities \"{0}\" and \"{1}\"", info.ThisEntityName, info.OtherEntityName));
                }
            }
        }