Esempio n. 1
0
        public void Can_set_owner_and_corresponding_association_added_to_model()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = database.AddTable("D");

            foreignKeyBuilder.SetOwner(dependentTable);

            var associationType = database.GetAssociationType("FK");

            Assert.NotNull(associationType);
            Assert.NotNull(associationType.SourceEnd);
            Assert.NotNull(associationType.TargetEnd);
            Assert.Same(principalTable, associationType.SourceEnd.GetEntityType());
            Assert.Equal("P", associationType.SourceEnd.Name);
            Assert.Same(dependentTable, associationType.TargetEnd.GetEntityType());
            Assert.Equal("D", associationType.TargetEnd.Name);

            var associationSet = database.GetAssociationSet(associationType);

            Assert.NotNull(associationSet);
            Assert.NotNull(associationSet.SourceSet);
            Assert.NotNull(associationSet.TargetSet);
        }
        public void Can_set_owner_and_corresponding_association_added_to_model()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = database.AddTable("D");

            foreignKeyBuilder.SetOwner(dependentTable);

            var associationType = database.GetAssociationType("FK");

            Assert.NotNull(associationType);
            Assert.NotNull(associationType.SourceEnd);
            Assert.NotNull(associationType.TargetEnd);
            Assert.Same(principalTable, associationType.SourceEnd.GetEntityType());
            Assert.Equal("P", associationType.SourceEnd.Name);
            Assert.Same(dependentTable, associationType.TargetEnd.GetEntityType());
            Assert.Equal("D", associationType.TargetEnd.Name);

            var associationSet = database.GetAssociationSet(associationType);

            Assert.NotNull(associationSet);
            Assert.NotNull(associationSet.SourceSet);
            Assert.NotNull(associationSet.TargetSet);
            
        }
Esempio n. 3
0
        public void Can_get_and_set_dependent_columns_and_multiplicities_assigned()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);

            var property = EdmProperty.CreatePrimitive("K", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            foreignKeyBuilder.DependentColumns = new[] { property };

            Assert.Equal(1, foreignKeyBuilder.DependentColumns.Count());

            var associationType = database.GetAssociationType("FK");

            Assert.NotNull(associationType.Constraint);
            Assert.Same(property, associationType.Constraint.ToProperties.Single());
            Assert.Same(associationType.SourceEnd, associationType.Constraint.FromRole);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.ToRole);
            Assert.Equal(RelationshipMultiplicity.Many, associationType.TargetEnd.RelationshipMultiplicity);
            Assert.Equal(RelationshipMultiplicity.ZeroOrOne, associationType.SourceEnd.RelationshipMultiplicity);

            property.Nullable = false;

            foreignKeyBuilder.DependentColumns = new[] { property };

            Assert.Equal(RelationshipMultiplicity.One, associationType.SourceEnd.RelationshipMultiplicity);
        }
Esempio n. 4
0
        public void Set_principal_table_when_self_ref_should_differentiate_target_end_name()
        {
            var database
                = new EdmModel
                {
                Version = 3.0
                }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            foreignKeyBuilder.SetOwner(table);

            var associationType = database.GetAssociationType("FK");

            Assert.Equal("P", associationType.SourceEnd.Name);
            Assert.Equal("T", associationType.TargetEnd.Name);

            foreignKeyBuilder.PrincipalTable = table;

            Assert.Same(table, associationType.SourceEnd.GetEntityType());
            Assert.Equal("T", associationType.SourceEnd.Name);
            Assert.Same(table, associationType.TargetEnd.GetEntityType());
            Assert.Equal("TSelf", associationType.TargetEnd.Name);
        }
        private static void GenerateForeignKeyAssociationType(
            AssociationType associationType, DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(associationType);
            DebugCheck.NotNull(databaseMapping);
            Debug.Assert(associationType.Constraint != null);

            var dependentEnd = associationType.Constraint.DependentEnd;
            var principalEnd = associationType.GetOtherEnd(dependentEnd);
            var principalEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, principalEnd.GetEntityType());
            var dependentEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType());

            var foreignKeyConstraint
                = new ForeignKeyBuilder(databaseMapping.Database, associationType.Name)
                    {
                        PrincipalTable =
                            principalEntityTypeMapping.MappingFragments.Single().Table,
                        DeleteAction = principalEnd.DeleteBehavior != OperationAction.None
                                           ? principalEnd.DeleteBehavior
                                           : OperationAction.None
                    };

            dependentEntityTypeMapping
                .MappingFragments
                .Single()
                .Table
                .AddForeignKey(foreignKeyConstraint);

            foreignKeyConstraint.DependentColumns = associationType.Constraint.ToProperties.Select(
                dependentProperty => dependentEntityTypeMapping.GetPropertyMapping(dependentProperty).ColumnProperty);

            foreignKeyConstraint.SetAssociationType(associationType);
        }
        public void Name_should_return_association_type_name()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            Assert.Equal("FK", foreignKeyBuilder.Name);
        }
Esempio n. 7
0
        public void Name_should_return_association_type_name()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            Assert.Equal("FK", foreignKeyBuilder.Name);
        }
Esempio n. 8
0
        internal void RemoveForeignKey(ForeignKeyBuilder foreignKeyBuilder)
        {
            DebugCheck.NotNull(foreignKeyBuilder);
            Util.ThrowIfReadOnly(this);

            foreignKeyBuilder.SetOwner(null);

            _foreignKeyBuilders.Remove(foreignKeyBuilder);
        }
Esempio n. 9
0
        internal void AddForeignKey(ForeignKeyBuilder foreignKeyBuilder)
        {
            DebugCheck.NotNull(foreignKeyBuilder);
            Util.ThrowIfReadOnly(this);

            foreignKeyBuilder.SetOwner(this);

            _foreignKeyBuilders.Add(foreignKeyBuilder);
        }
Esempio n. 10
0
        internal void RemoveForeignKey(ForeignKeyBuilder foreignKeyBuilder)
        {
            DebugCheck.NotNull(foreignKeyBuilder);
            Util.ThrowIfReadOnly(this);

            foreignKeyBuilder.SetOwner(null);

            _foreignKeyBuilders.Remove(foreignKeyBuilder);
        }
Esempio n. 11
0
        internal void AddForeignKey(ForeignKeyBuilder foreignKeyBuilder)
        {
            DebugCheck.NotNull(foreignKeyBuilder);
            Util.ThrowIfReadOnly(this);

            foreignKeyBuilder.SetOwner(this);

            _foreignKeyBuilders.Add(foreignKeyBuilder);
        }
Esempio n. 12
0
        public void Name_should_return_association_type_name()
        {
            var foreignKeyBuilder = new ForeignKeyBuilder(
                new EdmModel
            {
                Version = 3.0
            },
                "FK");

            Assert.Equal("FK", foreignKeyBuilder.Name);
        }
Esempio n. 13
0
        public void Name_should_return_association_type_name()
        {
            var foreignKeyBuilder = new ForeignKeyBuilder(
                new EdmModel
                    {
                        Version = 3.0
                    },
                "FK");

            Assert.Equal("FK", foreignKeyBuilder.Name);
        }
        public void Can_get_and_set_principal_table()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            Assert.Same(principalTable, foreignKeyBuilder.PrincipalTable);
        }
Esempio n. 15
0
        public void Can_get_and_set_principal_table()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            Assert.Same(principalTable, foreignKeyBuilder.PrincipalTable);
        }
Esempio n. 16
0
        public void Can_get_and_set_principal_table()
        {
            var foreignKeyBuilder = new ForeignKeyBuilder(
                new EdmModel
                    {
                        Version = 3.0
                    },
                "FK");

            var principalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = principalTable;

            Assert.Same(principalTable, foreignKeyBuilder.PrincipalTable);
        }
Esempio n. 17
0
        public void Can_get_and_set_principal_table()
        {
            var foreignKeyBuilder = new ForeignKeyBuilder(
                new EdmModel
            {
                Version = 3.0
            },
                "FK");

            var principalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = principalTable;

            Assert.Same(principalTable, foreignKeyBuilder.PrincipalTable);
        }
Esempio n. 18
0
        public void Can_get_and_set_delete_action()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);
            foreignKeyBuilder.DeleteAction = OperationAction.Cascade;

            var associationType = database.GetAssociationType("FK");

            Assert.Equal(OperationAction.Cascade, associationType.SourceEnd.DeleteBehavior);
        }
Esempio n. 19
0
        public void SetOwner_when_self_ref_should_differentiate_target_end_name()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);

            var associationType = database.GetAssociationType("FK");

            Assert.Same(table, associationType.SourceEnd.GetEntityType());
            Assert.Equal("T", associationType.SourceEnd.Name);
            Assert.Same(table, associationType.TargetEnd.GetEntityType());
            Assert.Equal("TSelf", associationType.TargetEnd.Name);
        }
Esempio n. 20
0
        public void Can_get_and_set_delete_action()
        {
            var database
                = new EdmModel
                {
                Version = 3.0
                }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);
            foreignKeyBuilder.DeleteAction = OperationAction.Cascade;

            var associationType = database.GetAssociationType("FK");

            Assert.Equal(OperationAction.Cascade, associationType.SourceEnd.DeleteBehavior);
        }
Esempio n. 21
0
        public void SetOwner_when_null_should_remove_association_type_from_model()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = database.AddTable("D");

            foreignKeyBuilder.SetOwner(dependentTable);

            Assert.NotNull(database.GetAssociationType("FK"));

            foreignKeyBuilder.SetOwner(null);

            Assert.Null(database.GetAssociationType("FK"));
        }
        public void SetOwner_when_null_should_remove_association_type_from_model()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = database.AddTable("P");

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = database.AddTable("D");

            foreignKeyBuilder.SetOwner(dependentTable);

            Assert.NotNull(database.GetAssociationType("FK"));

            foreignKeyBuilder.SetOwner(null);

            Assert.Null(database.GetAssociationType("FK"));
        }
Esempio n. 23
0
        public void AssociationSet_TargetEnd_correct_after_setting_owner()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            foreignKeyBuilder.PrincipalTable = database.AddTable("P");
            foreignKeyBuilder.SetOwner(database.AddTable("T"));

            var associationType = database.GetAssociationType("FK");
            var associationSet  = database.GetAssociationSet(associationType);

            Assert.Equal("T", associationType.TargetEnd.Name);
            Assert.Equal("T", associationSet.TargetEnd.Name);

            foreignKeyBuilder.SetOwner(database.AddTable("T1"));

            Assert.Equal("T1", associationType.TargetEnd.Name);
            Assert.Equal("T1", associationSet.TargetEnd.Name);
        }
        public void Apply_should_introduce_cascade_delete_on_constraints()
        {
            var databaseMapping
                = new DbDatabaseMapping()
                    .Initialize(new EdmModel().Initialize(), new EdmModel().Initialize());

            var foreignKeyConstraint
                = new ForeignKeyBuilder(databaseMapping.Database, "FK")
                      {
                          PrincipalTable
                              = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace)
                      };

            Assert.Equal(OperationAction.None, foreignKeyConstraint.DeleteAction);

            var table = databaseMapping.Database.AddTable("T");

            table.AddForeignKey(foreignKeyConstraint);

            var associationType
                = new AssociationType
                      {
                          SourceEnd = new AssociationEndMember("S", new EntityType()),
                          TargetEnd = new AssociationEndMember("T", new EntityType())
                      };

            associationType.SourceEnd.RelationshipMultiplicity = RelationshipMultiplicity.Many;

            associationType.TargetEnd.RelationshipMultiplicity = RelationshipMultiplicity.Many;

            var associationSetMapping = databaseMapping.AddAssociationSetMapping(
                new AssociationSet("AS", associationType), new EntitySet());

            associationSetMapping.StoreEntitySet = databaseMapping.Database.GetEntitySet(table);

            ((IDbMappingConvention)new ManyToManyCascadeDeleteConvention()).Apply(databaseMapping);

            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);
        }
        public void Configure_should_split_key_constraint_when_to_table_configuration()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var sourceTable = database.AddTable("Source");
            var principalTable = database.AddTable("P");

            var fkColumn
                = new EdmProperty(
                    "Fk",
                    ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            sourceTable.AddColumn(fkColumn);
            var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK")
                                           {
                                               PrincipalTable = principalTable
                                           };
            sourceTable.AddForeignKey(foreignKeyConstraint);
            foreignKeyConstraint.DependentColumns = new[] { fkColumn };
            var targetTable = database.AddTable("Split");
            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), database.GetEntitySet(sourceTable)).Initialize();
            associationSetMapping.SourceEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), fkColumn));

            var independentAssociationMappingConfiguration
                = new ForeignKeyAssociationMappingConfiguration();

            independentAssociationMappingConfiguration.ToTable("Split");

            independentAssociationMappingConfiguration.Configure(associationSetMapping, database, new MockPropertyInfo());

            Assert.True(targetTable.Properties.Contains(fkColumn));
            Assert.True(targetTable.ForeignKeyBuilders.Contains(foreignKeyConstraint));
            Assert.False(sourceTable.Properties.Contains(fkColumn));
            Assert.False(sourceTable.ForeignKeyBuilders.Contains(foreignKeyConstraint));
            Assert.Same(targetTable, associationSetMapping.Table);
        }
Esempio n. 26
0
        public void SetOwner_when_null_should_remove_association_type_from_model()
        {
            var database
                = new EdmModel
                {
                Version = 3.0
                }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = new EntityType("D", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.SetOwner(dependentTable);

            Assert.NotNull(database.GetAssociationType("FK"));

            foreignKeyBuilder.SetOwner(null);

            Assert.Null(database.GetAssociationType("FK"));
        }
Esempio n. 27
0
        public void SetOwner_when_null_should_remove_association_type_from_model()
        {
            var database
                = new EdmModel
                      {
                          Version = 3.0
                      }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var principalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = principalTable;

            var dependentTable = new EntityType("D", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.SetOwner(dependentTable);

            Assert.NotNull(database.GetAssociationType("FK"));

            foreignKeyBuilder.SetOwner(null);

            Assert.Null(database.GetAssociationType("FK"));
        }
        public static void AddTypeConstraint(
            EdmModel database,
            EntityType entityType,
            EntityType principalTable,
            EntityType dependentTable,
            bool isSplitting)
        {
            DebugCheck.NotNull(principalTable);
            DebugCheck.NotNull(dependentTable);
            DebugCheck.NotNull(entityType);

            var foreignKeyConstraintMetadata
                = new ForeignKeyBuilder(
                    database, String.Format(
                        CultureInfo.InvariantCulture,
                        "{0}_TypeConstraint_From_{1}_To_{2}",
                        entityType.Name,
                        principalTable.Name,
                        dependentTable.Name))
                    {
                        PrincipalTable = principalTable
                    };

            dependentTable.AddForeignKey(foreignKeyConstraintMetadata);

            if (isSplitting)
            {
                foreignKeyConstraintMetadata.SetIsSplitConstraint();
            }
            else
            {
                foreignKeyConstraintMetadata.SetIsTypeConstraint();
            }

            foreignKeyConstraintMetadata.DependentColumns = dependentTable.Properties.Where(c => c.IsPrimaryKeyColumn);

            //If "DbStoreGeneratedPattern.Identity" was copied from the parent table, it should be removed
            dependentTable.Properties.Where(c => c.IsPrimaryKeyColumn).Each(c => c.RemoveStoreGeneratedIdentityPattern());
        }
        private static void CopyForeignKeyConstraint(EdmModel database, EntityType toTable, ForeignKeyBuilder fk, 
            Func<EdmProperty, EdmProperty> selector = null)
        {
            DebugCheck.NotNull(toTable);
            DebugCheck.NotNull(fk);

            var newFk
                = new ForeignKeyBuilder(
                    database,
                    database.EntityTypes.SelectMany(t => t.ForeignKeyBuilders).UniquifyName(fk.Name))
                    {
                        PrincipalTable = fk.PrincipalTable,
                        DeleteAction = fk.DeleteAction
                    };

            newFk.SetPreferredName(fk.Name);

            var dependentColumns = 
                GetDependentColumns(
                    selector != null
                        ? fk.DependentColumns.Select(selector)
                        : fk.DependentColumns,
                    toTable.Properties);

            if (!ContainsEquivalentForeignKey(toTable, newFk.PrincipalTable, dependentColumns))
            {
                toTable.AddForeignKey(newFk);

                newFk.DependentColumns = dependentColumns;
            }
        }
        // <summary>
        // Moves a foreign key constraint from oldTable to newTable and updates column references
        // </summary>
        private static void MoveForeignKeyConstraint(
            EntityType fromTable, EntityType toTable, ForeignKeyBuilder fk)
        {
            DebugCheck.NotNull(fromTable);
            DebugCheck.NotNull(toTable);
            DebugCheck.NotNull(fk);

            fromTable.RemoveForeignKey(fk);

            // Only move it to the new table if the destination is not the principal table or if all dependent columns are not FKs
            // Otherwise you end up with an FK from the PKs to the PKs of the same table
            if (fk.PrincipalTable != toTable
                || !fk.DependentColumns.All(c => c.IsPrimaryKeyColumn))
            {
                // Make sure all the dependent columns refer to columns in the newTable
                var oldColumns = fk.DependentColumns.ToArray();

                var dependentColumns
                    = GetDependentColumns(oldColumns, toTable.Properties);

                if (!ContainsEquivalentForeignKey(toTable, fk.PrincipalTable, dependentColumns))
                {
                    toTable.AddForeignKey(fk);

                    fk.DependentColumns = dependentColumns;
                }
            }
        }
 internal void RemoveForeignKey(ForeignKeyBuilder foreignKeyBuilder)
 {
     Util.ThrowIfReadOnly((MetadataItem)this);
     foreignKeyBuilder.SetOwner((EntityType)null);
     this._foreignKeyBuilders.Remove(foreignKeyBuilder);
 }
        public void AssociationSet_TargetEnd_correct_after_setting_owner()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            foreignKeyBuilder.PrincipalTable = database.AddTable("P");
            foreignKeyBuilder.SetOwner(database.AddTable("T"));

            var associationType = database.GetAssociationType("FK");
            var associationSet = database.GetAssociationSet(associationType);

            Assert.Equal("T", associationType.TargetEnd.Name);
            Assert.Equal("T", associationSet.TargetEnd.Name);

            foreignKeyBuilder.SetOwner(database.AddTable("T1"));

            Assert.Equal("T1", associationType.TargetEnd.Name);
            Assert.Equal("T1", associationSet.TargetEnd.Name);
        }
Esempio n. 33
0
        public void Set_principal_table_when_self_ref_should_differentiate_target_end_name()
        {
            var database
                = new EdmModel
                      {
                          Version = 3.0
                      }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = new EntityType("P", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            foreignKeyBuilder.SetOwner(table);

            var associationType = database.GetAssociationType("FK");

            Assert.Equal("P", associationType.SourceEnd.Name);
            Assert.Equal("T", associationType.TargetEnd.Name);

            foreignKeyBuilder.PrincipalTable = table;

            Assert.Same(table, associationType.SourceEnd.GetEntityType());
            Assert.Equal("T", associationType.SourceEnd.Name);
            Assert.Same(table, associationType.TargetEnd.GetEntityType());
            Assert.Equal("TSelf", associationType.TargetEnd.Name);
        }
        public void Can_get_and_set_dependent_columns_and_multiplicities_assigned()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);

            var property = EdmProperty.Primitive("K", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            foreignKeyBuilder.DependentColumns = new[] { property };

            Assert.Equal(1, foreignKeyBuilder.DependentColumns.Count());

            var associationType = database.GetAssociationType("FK");

            Assert.NotNull(associationType.Constraint);
            Assert.Same(property, associationType.Constraint.ToProperties.Single());
            Assert.Same(associationType.SourceEnd, associationType.Constraint.FromRole);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.ToRole);
            Assert.Equal(RelationshipMultiplicity.Many, associationType.TargetEnd.RelationshipMultiplicity);
            Assert.Equal(RelationshipMultiplicity.ZeroOrOne, associationType.SourceEnd.RelationshipMultiplicity);

            property.Nullable = false;

            foreignKeyBuilder.DependentColumns = new[] { property };

            Assert.Equal(RelationshipMultiplicity.One, associationType.SourceEnd.RelationshipMultiplicity);
        }
        private static AssociationSetMapping CreateIAMapping(EdmModel database, EntityType dependentTable)
        {
            var fkColumn = new EdmProperty(
                "FK", ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            dependentTable.AddColumn(fkColumn);
            var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK")
            {
                PrincipalTable = database.AddTable("P")
            };

            dependentTable.AddForeignKey(foreignKeyConstraint);
            foreignKeyConstraint.DependentColumns = new[] { fkColumn };

            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)),
                    database.GetEntitySet(dependentTable)).Initialize();

            associationSetMapping.SourceEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), fkColumn));
            
            return associationSetMapping;
        }
        private void GenerateIndependentForeignKeyConstraint(
            DbDatabaseMapping databaseMapping,
            EntityType principalEntityType,
            EntityType dependentEntityType,
            EntityType dependentTable,
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping associationEndMapping,
            string name,
            AssociationEndMember principalEnd,
            bool isPrimaryKeyColumn = false)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(principalEntityType);
            DebugCheck.NotNull(dependentTable);
            DebugCheck.NotNull(associationEndMapping);
            DebugCheck.NotEmpty(name);

            var principalTable
                = GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType)
                    .MappingFragments
                    .Single()
                    .Table;

            var foreignKeyConstraint
                = new ForeignKeyBuilder(databaseMapping.Database, name)
                    {
                        PrincipalTable = principalTable,
                        DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None
                                           ? associationEndMapping.AssociationEnd.DeleteBehavior
                                           : OperationAction.None
                    };

            var principalNavigationProperty
                = databaseMapping.Model.EntityTypes
                                 .SelectMany(e => e.DeclaredNavigationProperties)
                                 .SingleOrDefault(n => n.ResultEnd == principalEnd);

            dependentTable.AddForeignKey(foreignKeyConstraint);

            foreignKeyConstraint.DependentColumns = GenerateIndependentForeignKeyColumns(
                principalEntityType,
                dependentEntityType,
                associationSetMapping,
                associationEndMapping,
                dependentTable,
                isPrimaryKeyColumn,
                principalNavigationProperty);
        }
        public void Can_get_and_set_delete_action()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);
            foreignKeyBuilder.DeleteAction = OperationAction.Cascade;

            var associationType = database.GetAssociationType("FK");

            Assert.Equal(OperationAction.Cascade, associationType.SourceEnd.DeleteBehavior);
        }
        public void SetOwner_when_self_ref_should_differentiate_target_end_name()
        {
            var database = new EdmModel(DataSpace.SSpace);

            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = database.AddTable("T");

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);

            var associationType = database.GetAssociationType("FK");

            Assert.Same(table, associationType.SourceEnd.GetEntityType());
            Assert.Equal("T", associationType.SourceEnd.Name);
            Assert.Same(table, associationType.TargetEnd.GetEntityType());
            Assert.Equal("TSelf", associationType.TargetEnd.Name);
        }
Esempio n. 39
0
        public void Can_get_and_set_delete_action()
        {
            var database
                = new EdmModel
                      {
                          Version = 3.0
                      }.DbInitialize();
            var foreignKeyBuilder = new ForeignKeyBuilder(database, "FK");

            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            foreignKeyBuilder.PrincipalTable = table;
            foreignKeyBuilder.SetOwner(table);
            foreignKeyBuilder.DeleteAction = OperationAction.Cascade;

            var associationType = database.GetAssociationType("FK");

            Assert.Equal(OperationAction.Cascade, associationType.SourceEnd.DeleteBehavior);
        }
 internal void AddForeignKey(ForeignKeyBuilder foreignKeyBuilder)
 {
     Util.ThrowIfReadOnly((MetadataItem)this);
     foreignKeyBuilder.SetOwner(this);
     this._foreignKeyBuilders.Add(foreignKeyBuilder);
 }