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 Configure_should_configure_inverse()
        {
            var inverseMockPropertyInfo = new MockPropertyInfo();
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo())
                                                      {
                                                          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());
        }
Exemple #3
0
        public virtual void SetOwner(EntityType owner)
        {
            Util.ThrowIfReadOnly(this);

            if (owner == null)
            {
                _database.RemoveAssociationType(_associationType);
            }
            else
            {
                _associationType.TargetEnd
                    = new AssociationEndMember(
                          owner != PrincipalTable ? owner.Name : owner.Name + SelfRefSuffix,
                          owner);

                _associationSet.TargetSet
                    = _database.GetEntitySet(owner);

                if (!_database.AssociationTypes.Contains(_associationType))
                {
                    _database.AddAssociationType(_associationType);
                    _database.AddAssociationSet(_associationSet);
                }
            }
        }
        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.CreatePrimitive("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 });

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void HasCascadeDeletePath_should_return_true_for_self_ref_cascade()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("A");
            var associationType
                = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
                      {
                          SourceEnd = new AssociationEndMember("S", entityType)
                      };
            associationType.TargetEnd = new AssociationEndMember("T", associationType.SourceEnd.GetEntityType());

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

            Assert.True(model.HasCascadeDeletePath(entityType, entityType));
        }
        private DataModelErrorEventArgs ValidateAssociationTypeWithNonFkeyReference(DataSpace dataSpace)
        {
            var model = new EdmModel(dataSpace, 1.0);

            var intType =
                dataSpace == DataSpace.CSpace
                    ? PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)
                    : FakeSqlProviderServices.Instance.GetProviderManifest("2008").GetStoreTypes().Single(t => t.Name == "int");

            var principal =
                new EntityType("P", "ns", dataSpace, new [] { "Id" }, new[] { EdmProperty.CreatePrimitive("Id", intType) });
            var dependent =
                new EntityType("P", "ns", dataSpace, new [] { "Id" },
                               new[] { EdmProperty.CreatePrimitive("Id", intType), EdmProperty.CreatePrimitive("NonKeyProperty", intType) });

            foreach (var property in principal.Properties.Concat(dependent.Properties))
            {
                property.Nullable = false;
            }

            var associationType =
                new AssociationType("AT", "ns", false, dataSpace)
            {
                Constraint = new ReferentialConstraint(
                    new AssociationEndMember("P", principal.GetReferenceType(), RelationshipMultiplicity.One),
                    new AssociationEndMember("C", dependent.GetReferenceType(), RelationshipMultiplicity.Many),
                    principal.KeyProperties,
                    dependent.Properties.Where(p => p.Name == "NonKeyProperty"))
            };

            model.AddAssociationType(associationType);

            var validationContext = new EdmModelValidationContext(model, true);

            DataModelErrorEventArgs errorEventArgs = null;

            validationContext.OnError += (_, e) => errorEventArgs = e;

            EdmModelSemanticValidationRules
            .EdmAssociationType_ValidateReferentialConstraint
            .Evaluate(validationContext, model.AssociationTypes.Single());

            return(errorEventArgs);
        }
        public void Apply_generates_constraint_when_simple_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("Fk", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);
            var fkProperty = property;
            var foreignKeyAnnotation = new ForeignKeyAttribute("Fk");
            navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });

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

            Assert.NotNull(associationType.Constraint);
            Assert.True(associationType.Constraint.ToProperties.Contains(fkProperty));
        }
        public void Generate_can_map_foreign_key_association_type()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var principalEntityType = model.AddEntityType("P");
            principalEntityType.GetMetadataProperties().SetClrType(typeof(object));

            var dependentEntityType = model.AddEntityType("D");
            dependentEntityType.GetMetadataProperties().SetClrType(typeof(string));

            var dependentProperty1 = EdmProperty.CreatePrimitive("FK1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            dependentProperty1.Nullable = false;
            dependentEntityType.AddMember(dependentProperty1);

            var dependentProperty2 = EdmProperty.CreatePrimitive("FK2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            dependentEntityType.AddMember(dependentProperty2);

            model.AddEntitySet("PSet", principalEntityType);
            model.AddEntitySet("DSet", dependentEntityType);

            var associationType
                = model.AddAssociationType(
                    "P_D",
                    principalEntityType, RelationshipMultiplicity.One,
                    dependentEntityType, RelationshipMultiplicity.Many);

            associationType.Constraint
                = new ReferentialConstraint(
                    associationType.SourceEnd,
                    associationType.TargetEnd,
                    principalEntityType.KeyProperties,
                    new[] { dependentProperty1, dependentProperty2 });

            associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;

            var databaseMapping
                = CreateDatabaseMappingGenerator().Generate(model);

            var dependentTable = databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table;
            var foreignKeyConstraint = dependentTable.ForeignKeyBuilders.Single();

            Assert.Equal(2, dependentTable.Properties.Count());
            Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);
            Assert.Equal(associationType.Name, foreignKeyConstraint.Name);

            var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();

            Assert.False(foreignKeyColumn.Nullable);
            Assert.Equal("FK1", foreignKeyColumn.Name);
        }
        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.GetMetadataProperties().SetClrType(typeof(string));
            model.AddEntitySet("E1Set", entityType1);

            var entityType2 = model.AddEntityType("E2");
            entityType2.GetMetadataProperties().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
                = CreateDatabaseMappingGenerator().Generate(model);

            Assert.Equal(0, databaseMapping.Database.Functions.Count());
        }
        private static EdmModel CreateModelFixture(
            out EntityType declaringEntityType, out EntityType complexEntityType)
        {
            var model = new EdmModel(DataSpace.CSpace);

            declaringEntityType = model.AddEntityType("E");
            complexEntityType = model.AddEntityType("C");
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            complexEntityType.AddMember(property);

            var associationType
                = model.AddAssociationType(
                    "A",
                    declaringEntityType, RelationshipMultiplicity.Many,
                    complexEntityType, RelationshipMultiplicity.ZeroOrOne);

            declaringEntityType.AddNavigationProperty("E.C", associationType);

            return model;
        }
        public void Generate_can_map_independent_association_type()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var principalEntityType = model.AddEntityType("P");
            var type = typeof(object);

            principalEntityType.GetMetadataProperties().SetClrType(type);
            var property = EdmProperty.CreatePrimitive("Id1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property);
            var idProperty1 = property;
            principalEntityType.AddKeyMember(idProperty1);
            var property1 = EdmProperty.CreatePrimitive("Id2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property1);
            var idProperty2 = property1;
            principalEntityType.AddKeyMember(idProperty2);
            var dependentEntityType = model.AddEntityType("D");
            var type1 = typeof(string);

            dependentEntityType.GetMetadataProperties().SetClrType(type1);
            model.AddEntitySet("PSet", principalEntityType);
            model.AddEntitySet("DSet", dependentEntityType);
            var associationType
                = model.AddAssociationType(
                    "P_D",
                    principalEntityType, RelationshipMultiplicity.One,
                    dependentEntityType, RelationshipMultiplicity.Many);
            model.AddAssociationSet("P_DSet", associationType);
            associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;

            var databaseMapping = CreateDatabaseMappingGenerator().Generate(model);

            var foreignKeyConstraint
                =
                databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table.ForeignKeyBuilders.Single();

            Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
            Assert.Equal(associationType.Name, foreignKeyConstraint.Name);
            Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().AssociationSetMappings.Count());
            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);

            var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();

            Assert.False(foreignKeyColumn.Nullable);
            Assert.Equal("P_Id1", foreignKeyColumn.Name);
        }
        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.CreatePrimitive("Fk1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

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

            entityType.AddMember(property1);
            var fkProperty2 = property1;
            var foreignKeyAnnotation = new ForeignKeyAttribute("Fk2,Fk1");
            navigationProperty.GetMetadataProperties().SetClrAttributes(new[] { foreignKeyAnnotation });

            (new ForeignKeyNavigationPropertyAttributeConvention())
                .Apply(navigationProperty, new DbModel(model, null));

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

            entityType.AddKeyMember(property);

            var targetConfig = new EntityTypeConfiguration(typeof(object));
            targetConfig.ToTable("SharedTable");
            entityType.GetMetadataProperties().SetConfiguration(targetConfig);

            var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
            var sourceConfig = new EntityTypeConfiguration(typeof(object));
            sourceConfig.ToTable("SharedTable");
            sourceEntityType.GetMetadataProperties().SetConfiguration(sourceConfig);

            var associationType
                = model.AddAssociationType(
                    "A", sourceEntityType, RelationshipMultiplicity.One,
                    entityType, RelationshipMultiplicity.One);

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

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void AddAssociationSet_should_create_and_add_to_default_container()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var sourceEntityType = model.AddEntityType("Source");
            var targetEntityType = model.AddEntityType("Target");

            model.AddEntitySet("S", sourceEntityType);
            model.AddEntitySet("T", targetEntityType);

            var associationType = model.AddAssociationType(
                "Foo",
                sourceEntityType, RelationshipMultiplicity.One,
                targetEntityType, RelationshipMultiplicity.Many);

            var associationSet = model.AddAssociationSet("FooSet", associationType);

            Assert.NotNull(associationSet);
            Assert.Equal("FooSet", associationSet.Name);
            Assert.Same(associationType, associationSet.ElementType);

            Assert.True(model.Containers.Single().AssociationSets.Contains(associationSet));
        }
        public void Apply_should_match_optional_self_ref()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            model.AddAssociationType(
                    "A",
                    entityType, RelationshipMultiplicity.ZeroOrOne,
                    entityType, RelationshipMultiplicity.Many);

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.NotNull(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Apply_throws_when_cannot_find_foreign_key_properties()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("E");
            var mockType = new MockType();

            entityType.GetMetadataProperties().SetClrType(mockType);
            var associationType = model.AddAssociationType(
                "A",
                entityType, RelationshipMultiplicity.ZeroOrOne,
                entityType, RelationshipMultiplicity.Many);
            var navigationProperty = entityType.AddNavigationProperty("N", associationType);
            var foreignKeyAnnotation = new ForeignKeyAttribute("_Fk");
            navigationProperty.GetMetadataProperties().SetClrAttributes(new[] { foreignKeyAnnotation });

            Assert.Equal(
                Strings.ForeignKeyAttributeConvention_InvalidKey("N", mockType.Object, "_Fk", mockType.Object),
                Assert.Throws<InvalidOperationException>(
                    () => (new ForeignKeyNavigationPropertyAttributeConvention())
                              .Apply(navigationProperty, new DbModel(model, null))).Message);
        }
        public void AddAssociationType_should_create_and_add_with_custom_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var sourceEntityType = model.AddEntityType("Source");
            var targetEntityType = model.AddEntityType("Target");

            var associationType = model.AddAssociationType(
                "Foo",
                sourceEntityType, RelationshipMultiplicity.One,
                targetEntityType, RelationshipMultiplicity.Many,
                "Bar");

            Assert.NotNull(associationType);
            Assert.Equal("Foo", associationType.Name);
            Assert.Equal("Bar", associationType.NamespaceName);
            Assert.Same(sourceEntityType, associationType.SourceEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.One, associationType.SourceEnd.RelationshipMultiplicity);
            Assert.Same(targetEntityType, associationType.TargetEnd.GetEntityType());
            Assert.Equal(RelationshipMultiplicity.Many, associationType.TargetEnd.RelationshipMultiplicity);
            Assert.True(model.AssociationTypes.Contains(associationType));
        }
        public void Apply_throws_with_empty_foreign_key_properties()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("E");
            var mockType = new MockType();

            entityType.Annotations.SetClrType(mockType);
            var associationType = model.AddAssociationType(
                "A",
                entityType, RelationshipMultiplicity.ZeroOrOne,
                entityType, RelationshipMultiplicity.Many);
            var navigationProperty = entityType.AddNavigationProperty("N", associationType);
            var foreignKeyAnnotation = new ForeignKeyAttribute(",");
            navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });

            Assert.Equal(
                Strings.ForeignKeyAttributeConvention_EmptyKey("N", mockType.Object),
                Assert.Throws<InvalidOperationException>(
                    () => ((IEdmConvention<NavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
                              .Apply(navigationProperty, model)).Message);
        }
        public void GetAssociationsBetween_should_return_matching_associations()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var entityTypeA = model.AddEntityType("Foo");
            var entityTypeB = model.AddEntityType("Bar");

            Assert.Equal(0, model.GetAssociationTypesBetween(entityTypeA, entityTypeB).Count());

            model.AddAssociationType(
                "Foo_Bar",
                entityTypeA, RelationshipMultiplicity.ZeroOrOne,
                entityTypeB, RelationshipMultiplicity.Many);

            model.AddAssociationType(
                "Bar_Foo",
                entityTypeB, RelationshipMultiplicity.ZeroOrOne,
                entityTypeA, RelationshipMultiplicity.Many);

            Assert.Equal(2, model.GetAssociationTypesBetween(entityTypeA, entityTypeB).Count());
            Assert.Equal(2, model.GetAssociationTypesBetween(entityTypeB, entityTypeA).Count());
            Assert.Equal(0, model.GetAssociationTypesBetween(entityTypeA, entityTypeA).Count());
        }
        public void Configure_should_uniquify_unconfigured_association_function_names()
        {
            var mockPropertyInfo = typeof(BType1).GetDeclaredProperty("As");

            var modelConfiguration = new ModelConfiguration();

            var navigationPropertyConfiguration
                = modelConfiguration.Entity(typeof(BType1)).Navigation(mockPropertyInfo);

            navigationPropertyConfiguration.ModificationStoredProceduresConfiguration
                = new ModificationStoredProceduresConfiguration();

            var modificationFunctionConfiguration = new ModificationStoredProcedureConfiguration();
            modificationFunctionConfiguration.HasName("AB_Delete");

            navigationPropertyConfiguration.ModificationStoredProceduresConfiguration
                .Insert(modificationFunctionConfiguration);

            var model = new EdmModel(DataSpace.CSpace);

            var entityA = model.AddEntityType("A");
            entityA.GetMetadataProperties().SetClrType(typeof(AType1));
            entityA.SetConfiguration(modelConfiguration.Entity(typeof(AType1)));

            var entityB = model.AddEntityType("B");
            entityB.GetMetadataProperties().SetClrType(typeof(BType1));
            entityB.SetConfiguration(modelConfiguration.Entity(typeof(BType1)));

            model.AddEntitySet("AS", entityA);
            model.AddEntitySet("BS", entityB);

            var associationType
                = model.AddAssociationType(
                    "M2M",
                    entityA,
                    RelationshipMultiplicity.Many,
                    entityB,
                    RelationshipMultiplicity.Many);

            associationType.SetConfiguration(navigationPropertyConfiguration);

            var navigationProperty
                = entityB.AddNavigationProperty("As", associationType);

            navigationProperty.SetClrPropertyInfo(mockPropertyInfo);

            model.AddAssociationSet("M2MSet", associationType);

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

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete"));
            Assert.True(databaseMapping.Database.Functions.Any(f => f.StoreFunctionNameAttribute == "AB_Delete1"));
        }
        public void Configure_should_uniquify_unconfigured_assocation_function_names()
        {
            var typeA = new MockType("A");
            var typeB = new MockType("B").Property(typeA.AsCollection(), "As");
            var mockPropertyInfo = typeB.GetProperty("As");
            typeA.Property(typeB.AsCollection(), "Bs");

            var modelConfiguration = new ModelConfiguration();

            var navigationPropertyConfiguration
                = modelConfiguration.Entity(typeB).Navigation(mockPropertyInfo);

            navigationPropertyConfiguration.ModificationFunctionsConfiguration
                = new ModificationFunctionsConfiguration();

            var modificationFunctionConfiguration = new ModificationFunctionConfiguration();
            modificationFunctionConfiguration.HasName("M2M_Delete");

            navigationPropertyConfiguration.ModificationFunctionsConfiguration
                .Insert(modificationFunctionConfiguration);

            var model = new EdmModel(DataSpace.CSpace);

            var entityA = model.AddEntityType("A");
            entityA.Annotations.SetClrType(typeA);
            entityA.SetConfiguration(modelConfiguration.Entity(typeA));

            var entityB = model.AddEntityType("B");
            entityB.Annotations.SetClrType(typeB);
            entityB.SetConfiguration(modelConfiguration.Entity(typeB));

            model.AddEntitySet("AS", entityA);
            model.AddEntitySet("BS", entityB);

            var associationType
                = model.AddAssociationType(
                    "M2M",
                    entityA,
                    RelationshipMultiplicity.Many,
                    entityB,
                    RelationshipMultiplicity.Many);

            associationType.SetConfiguration(navigationPropertyConfiguration);

            var navigationProperty
                = entityB.AddNavigationProperty("As", associationType);

            navigationProperty.SetClrPropertyInfo(mockPropertyInfo);

            model.AddAssociationSet("M2MSet", associationType);

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

            modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest);

            Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete"));
            Assert.True(databaseMapping.Database.Functions.Any(f => f.Name == "M2M_Delete1"));
        }
        private DataModelErrorEventArgs ValidateAssociationTypeWithNonFkeyReference(DataSpace dataSpace)
        {
            var model = new EdmModel(dataSpace, 1.0);

            var intType =
                dataSpace == DataSpace.CSpace
                    ? PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)
                    : FakeSqlProviderServices.Instance.GetProviderManifest("2008").GetStoreTypes().Single(t => t.Name == "int");

            var principal = 
                new EntityType("P", "ns", dataSpace, new [] {"Id"}, new[] { EdmProperty.CreatePrimitive("Id", intType) });
            var dependent = 
                new EntityType("P", "ns", dataSpace, new [] {"Id"},
                    new[] { EdmProperty.CreatePrimitive("Id", intType), EdmProperty.CreatePrimitive("NonKeyProperty", intType) });

            foreach (var property in principal.Properties.Concat(dependent.Properties))
            {
                property.Nullable = false;
            }

            var associationType =
                new AssociationType("AT", "ns", false, dataSpace)
                    {
                        Constraint = new ReferentialConstraint(
                                new AssociationEndMember("P", principal.GetReferenceType(), RelationshipMultiplicity.One),
                                new AssociationEndMember("C", dependent.GetReferenceType(), RelationshipMultiplicity.Many),
                                principal.KeyProperties,
                                dependent.Properties.Where(p => p.Name == "NonKeyProperty"))
                    };

            model.AddAssociationType(associationType);

            var validationContext = new EdmModelValidationContext(model, true);

            DataModelErrorEventArgs errorEventArgs = null;
            validationContext.OnError += (_, e) => errorEventArgs = e;

            EdmModelSemanticValidationRules
                .EdmAssociationType_ValidateReferentialConstraint
                .Evaluate(validationContext, model.AssociationTypes.Single());

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

            var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
            var targetEntityType = new EntityType("E", "N", DataSpace.CSpace);

            model.AddEntitySet("S", sourceEntityType);
            model.AddEntitySet("T", targetEntityType);

            var associationType
                = model.AddAssociationType(
                    "A",
                    sourceEntityType, RelationshipMultiplicity.ZeroOrOne,
                    targetEntityType, RelationshipMultiplicity.Many);

            model.AddAssociationSet("FooSet", associationType);

            model.RemoveAssociationType(associationType);

            Assert.Equal(0, model.AssociationTypes.Count());
            Assert.Equal(0, model.Containers.First().AssociationSets.Count());
        }
        private EdmModel CreateModelWithAssociations(params EdmProperty[] foreignKeys)
        {
            var model = new EdmModel(DataSpace.SSpace);

            for (var i = 0; i < foreignKeys.Length; i++)
            {
                var sourceEntityType = new EntityType("E" + i, "N", DataSpace.SSpace);
                var targetEntityType = new EntityType("E" + i, "N", DataSpace.SSpace);

                model.AddEntitySet("S" + i, sourceEntityType);
                model.AddEntitySet("T" + i, targetEntityType);

                var fk = foreignKeys[i];
                var associationType = new AssociationType((fk == null ? "IA" : fk.Name) + i, "MN", false, DataSpace.SSpace)
                {
                    SourceEnd = new AssociationEndMember(
                        "A_Source" + i, sourceEntityType.GetReferenceType(), RelationshipMultiplicity.ZeroOrOne),
                    TargetEnd = new AssociationEndMember(
                        "A_Target" + i, targetEntityType.GetReferenceType(), RelationshipMultiplicity.Many)
                };

                model.AddAssociationType(associationType);

                if (fk != null)
                {
                    var constraint = new ReferentialConstraint(
                        associationType.SourceEnd,
                        associationType.TargetEnd,
                        new[] { new EdmProperty("SourceProperty") },
                        new[] { fk });

                    associationType.Constraint = constraint;
                }

                model.AddAssociationSet("Set" + (fk == null ? "IA" : fk.Name) + i, associationType);
            }

            return model;
        }