Esempio n. 1
0
        public void HasCascadeDeletePath_should_return_true_for_transitive_cascade()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var entityTypeA = model.AddEntityType("A");
            var entityTypeB = model.AddEntityType("B");
            var entityTypeC = model.AddEntityType("B");
            var associationTypeA
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", entityTypeA),
                TargetEnd = new AssociationEndMember("T", entityTypeB)
                };

            associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeA);
            var associationTypeB
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                {
                SourceEnd = new AssociationEndMember("S", entityTypeB),
                TargetEnd = new AssociationEndMember("T", entityTypeC)
                };

            associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeB);

            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB));
            Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC));
            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC));
            Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA));
        }
        public void HasCascadeDeletePath_should_return_true_for_transitive_cascade()
        {
            var model            = new EdmModel().Initialize();
            var entityTypeA      = model.AddEntityType("A");
            var entityTypeB      = model.AddEntityType("B");
            var entityTypeC      = model.AddEntityType("B");
            var associationTypeA = new AssociationType();

            associationTypeA.SourceEnd = new AssociationEndMember("S", entityTypeA);
            associationTypeA.TargetEnd = new AssociationEndMember("T", entityTypeB);

            associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeA);
            var associationTypeB = new AssociationType();

            associationTypeB.SourceEnd = new AssociationEndMember("S", entityTypeB);
            associationTypeB.TargetEnd = new AssociationEndMember("T", entityTypeC);

            associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade;
            model.AddAssociationType(associationTypeB);

            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB));
            Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC));
            Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC));
            Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB));
            Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA));
        }
Esempio n. 3
0
        public TestModelBuilder Association(
            string sourceEntity, RelationshipMultiplicity sourceEndKind,
            string targetEntity, RelationshipMultiplicity targetEndKind)
        {
            _model.AddAssociationSet(
                "AssociationSet" + _counter++,
                _model.AddAssociationType(
                    "Association",
                    _model.GetEntityType(sourceEntity), sourceEndKind,
                    _model.GetEntityType(targetEntity), targetEndKind));

            return(this);
        }
        public void Generate_should_not_generate_modification_function_mappings_when_ends_not_mapped_to_functions()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityType1 = model.AddEntityType("E1");

            entityType1.Annotations.SetClrType(typeof(string));
            model.AddEntitySet("E1Set", entityType1);

            var entityType2 = model.AddEntityType("E2");

            entityType2.Annotations.SetClrType(typeof(string));
            model.AddEntitySet("E2Set", entityType2);

            var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object));

            entityType1.SetConfiguration(entityTypeConfiguration);
            entityType2.SetConfiguration(entityTypeConfiguration);

            model.AddAssociationSet(
                "M2MSet",
                model.AddAssociationType(
                    "M2M",
                    entityType1,
                    RelationshipMultiplicity.Many,
                    entityType2,
                    RelationshipMultiplicity.Many));

            var databaseMapping
                = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(model);

            Assert.Equal(0, databaseMapping.Database.Functions.Count());
        }
        public static AssociationType AddAssociationType(
            this EdmModel model,
            string name,
            EntityType sourceEntityType,
            RelationshipMultiplicity sourceAssociationEndKind,
            EntityType targetEntityType,
            RelationshipMultiplicity targetAssociationEndKind,
            string modelNamespace = null)
        {
            DebugCheck.NotNull(model);
            DebugCheck.NotEmpty(name);
            DebugCheck.NotNull(sourceEntityType);
            DebugCheck.NotNull(targetEntityType);

            var associationType
                = new AssociationType(
                      name,
                      modelNamespace ?? DefaultModelNamespace,
                      false,
                      DataSpace.CSpace)
                {
                SourceEnd =
                    new AssociationEndMember(
                        name + "_Source", sourceEntityType.GetReferenceType(), sourceAssociationEndKind),
                TargetEnd =
                    new AssociationEndMember(
                        name + "_Target", targetEntityType.GetReferenceType(), targetAssociationEndKind)
                };

            model.AddAssociationType(associationType);

            return(associationType);
        }
        public void Configure_should_configure_inverse()
        {
            var inverseMockPropertyInfo         = new MockPropertyInfo();
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo(typeof(AType1), "N"))
            {
                InverseNavigationProperty = inverseMockPropertyInfo
            };
            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            var inverseAssociationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);

            inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
            inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
            var model = new EdmModel(DataSpace.CSpace);

            model.AddAssociationType(inverseAssociationType);
            var inverseNavigationProperty
                = model.AddEntityType("T")
                  .AddNavigationProperty("N", inverseAssociationType);

            inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo);

            navigationPropertyConfiguration.Configure(
                new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
            {
                RelationshipType = associationType
            }, model, new EntityTypeConfiguration(typeof(object)));

            Assert.Same(associationType, inverseNavigationProperty.Association);
            Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd);
            Assert.Same(associationType.TargetEnd, inverseNavigationProperty.FromEndMember);
            Assert.Equal(0, model.AssociationTypes.Count());
        }
        public void Apply_generates_constraint_when_composite_fk()
        {
            var model           = new EdmModel(DataSpace.CSpace);
            var entityType      = model.AddEntityType("E");
            var associationType = model.AddAssociationType(
                "A",
                entityType, RelationshipMultiplicity.ZeroOrOne,
                entityType, RelationshipMultiplicity.Many);
            var navigationProperty = entityType.AddNavigationProperty("N", associationType);
            var property           = EdmProperty.Primitive("Fk1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);
            var fkProperty1 = property;
            var property1   = EdmProperty.Primitive("Fk2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var fkProperty2          = property1;
            var foreignKeyAnnotation = new ForeignKeyAttribute("Fk2,Fk1");

            navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });

            ((IEdmConvention <NavigationProperty>) new ForeignKeyNavigationPropertyAttributeConvention())
            .Apply(navigationProperty, model);

            Assert.NotNull(associationType.Constraint);
            Assert.True(new[] { fkProperty2, fkProperty1 }.SequenceEqual(associationType.Constraint.ToProperties));
        }
        public void Apply_should_not_match_key_that_is_also_an_fk()
        {
            var model      = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            var associationType
                = model.AddAssociationType(
                      "A", new EntityType("E", "N", DataSpace.CSpace), RelationshipMultiplicity.ZeroOrOne,
                      entityType, RelationshipMultiplicity.Many);

            associationType.Constraint
                = new ReferentialConstraint(
                      associationType.SourceEnd,
                      associationType.TargetEnd,
                      new[] { property },
                      new[] { property });

            ((IEdmConvention <EntityType>) new StoreGeneratedIdentityKeyConvention())
            .Apply(entityType, model);

            Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
Esempio n. 9
0
        public static AssociationType AddAssociationType(
            this EdmModel model,
            string name,
            EntityType sourceEntityType,
            RelationshipMultiplicity sourceAssociationEndKind,
            EntityType targetEntityType,
            RelationshipMultiplicity targetAssociationEndKind)
        {
            DebugCheck.NotNull(model);
            DebugCheck.NotEmpty(name);
            DebugCheck.NotNull(sourceEntityType);
            DebugCheck.NotNull(targetEntityType);
            Debug.Assert(model.Namespaces.Count == 1);

            var associationType
                = new AssociationType(
                      name,
                      XmlConstants.GetCsdlNamespace(model.Version),
                      false,
                      DataSpace.CSpace)
                {
                SourceEnd =
                    new AssociationEndMember(
                        name + "_Source", new RefType(sourceEntityType), sourceAssociationEndKind),
                TargetEnd =
                    new AssociationEndMember(
                        name + "_Target", new RefType(targetEntityType), targetAssociationEndKind)
                };

            model.AddAssociationType(associationType);

            return(associationType);
        }
Esempio n. 10
0
        public void Configure_should_configure_inverse()
        {
            var inverseMockPropertyInfo         = new MockPropertyInfo();
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo())
            {
                InverseNavigationProperty = inverseMockPropertyInfo
            };
            var associationType = new AssociationType();

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

            inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType());
            inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType());
            var model = new EdmModel().Initialize();

            model.AddAssociationType(inverseAssociationType);
            var inverseNavigationProperty
                = model.AddEntityType("T")
                  .AddNavigationProperty("N", inverseAssociationType);

            inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo);

            navigationPropertyConfiguration.Configure(
                new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
            {
                RelationshipType = associationType
            }, model, new EntityTypeConfiguration(typeof(object)));

            Assert.Same(associationType, inverseNavigationProperty.Association);
            Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd);
            Assert.Equal(0, model.GetAssociationTypes().Count());
        }