コード例 #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 Configure_should_rename_table_when_table_configured()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var table = database.AddTable("OriginalName");

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

            var manyToManyAssociationMappingConfiguration
                = new ManyToManyAssociationMappingConfiguration();

            manyToManyAssociationMappingConfiguration.ToTable("NewName");

            var mockPropertyInfo = new MockPropertyInfo();

            associationSetMapping.SourceEndMapping.EndMember = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationSetMapping.SourceEndMapping.EndMember.SetClrPropertyInfo(mockPropertyInfo);

            manyToManyAssociationMappingConfiguration.Configure(associationSetMapping, database, mockPropertyInfo);

            Assert.Equal("NewName", table.GetTableName().Name);
            Assert.Same(manyToManyAssociationMappingConfiguration, table.GetConfiguration());
        }
コード例 #3
0
        public void AddTable_should_create_and_add_table_to_default_schema()
        {
            var database = new EdmModel(DataSpace.SSpace);

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

            Assert.True(database.EntityTypes.Contains(table));
            Assert.Equal("T", database.EntityTypes.First().Name);
        }
コード例 #4
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"));
        }
コード例 #5
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);
        }
コード例 #6
0
        public void AssociationSet_SourceEnd_correct_after_setting_principal_table()
        {
            var database = new EdmModel(DataSpace.SSpace);

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

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

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

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

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

            foreignKeyBuilder.PrincipalTable = table;

            Assert.Equal("T", associationType.SourceEnd.Name);
            Assert.Equal("T", associationSet.SourceEnd.Name);
        }
        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);
        }
コード例 #8
0
        public void Set_principal_table_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 = database.AddTable("P");
            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);
        }
コード例 #9
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);
        }
        public void Configure_should_apply_annotations_to_FK_columns()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var dependentTable = database.AddTable("Source");
            var associationSetMapping = CreateIAMapping(database, dependentTable);

            var configuration = new ForeignKeyAssociationMappingConfiguration();
            configuration.MapKey("K1")
                .HasColumnAnnotation("K1", "A1", "V1")
                .HasColumnAnnotation("K1", "A2", "V2")
                .HasColumnAnnotation("K1", "A1", "V3");

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

            var column = dependentTable.Properties.Single(p => p.Name == "K1");

            Assert.Equal("V3", column.Annotations.Single(a => a.Name == XmlConstants.CustomAnnotationPrefix + "A1").Value);
            Assert.Equal("V2", column.Annotations.Single(a => a.Name == XmlConstants.CustomAnnotationPrefix + "A2").Value);
        }
コード例 #11
0
        public void ApplyDatabase_should_run_targeted_model_conventions()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var table = database.AddTable("T");
            var mockConvention = new Mock<IDbConvention<EntityType>>();
            var conventionsConfiguration = new ConventionsConfiguration(
                new IConvention[]
                    {
                        mockConvention.Object
                    });
            
            conventionsConfiguration.ApplyDatabase(database);

            mockConvention.Verify(c => c.Apply(table, database), Times.AtMostOnce());
        }
コード例 #12
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);
        }
        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;
        }
コード例 #14
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);
        }
コード例 #15
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.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);
        }
コード例 #16
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 Configure_should_throw_when_annotation_key_name_not_found()
        {
            var database = new EdmModel(DataSpace.SSpace);
            var dependentTable = database.AddTable("Source");
            var associationSetMapping = CreateIAMapping(database, dependentTable);

            var configuration = new ForeignKeyAssociationMappingConfiguration();
            configuration.MapKey("K1").HasColumnAnnotation("BadKey", "A1", "V1");

            Assert.Equal(
                Strings.BadKeyNameForAnnotation("BadKey", "A1"),
                Assert.Throws<InvalidOperationException>(
                    () => configuration.Configure(associationSetMapping, database, new MockPropertyInfo())).Message);

        }