Beispiel #1
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 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());
        }
        public void MapEntityType_should_not_bring_in_base_class_by_default()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var typeMapper = new TypeMapper(new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), model));

            var entityType = typeMapper.MapEntityType(typeof(AType3));

            Assert.NotNull(entityType);
            Assert.Null(entityType.BaseType);
            Assert.Equal(1, model.EntityTypes.Count());
            Assert.Equal(1, model.Containers.Single().EntitySets.Count);
            Assert.Equal("AType3", model.GetEntitySet(entityType).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);
        }
        internal override void Configure(
            AssociationSetMapping associationSetMapping, EdmModel database, PropertyInfo navigationProperty)
        {
            DebugCheck.NotNull(associationSetMapping);

            DebugCheck.NotNull(navigationProperty);

            // By convention source end contains the dependent column mappings
            var propertyMappings = associationSetMapping.SourceEndMapping.PropertyMappings.ToList();

            if (_tableName != null)
            {
                var targetTable
                    = ((from t in database.EntityTypes
                        let n = t.GetTableName()
                        where (n != null && n.Equals(_tableName))
                        select t)
                          .SingleOrDefault())
                      ?? (from es in database.GetEntitySets()
                          where string.Equals(es.Table, _tableName.Name, StringComparison.Ordinal)
                          select es.ElementType).SingleOrDefault();

                if (targetTable == null)
                {
                    throw Error.TableNotFound(_tableName);
                }

                var sourceTable = associationSetMapping.Table;

                if (sourceTable != targetTable)
                {
                    var foreignKeyConstraint
                        = sourceTable.ForeignKeyBuilders
                                     .Single(fk => fk.DependentColumns.SequenceEqual(propertyMappings.Select(pm => pm.Column)));

                    sourceTable.RemoveForeignKey(foreignKeyConstraint);
                    targetTable.AddForeignKey(foreignKeyConstraint);

                    foreignKeyConstraint.DependentColumns
                                        .Each(
                                            c =>
                                            {
                                                var isKey = c.IsPrimaryKeyColumn;

                                                sourceTable.RemoveMember(c);
                                                targetTable.AddMember(c);

                                                if (isKey)
                                                {
                                                    targetTable.AddKeyMember(c);
                                                }
                                            });

                    associationSetMapping.StoreEntitySet = database.GetEntitySet(targetTable);
                }
            }

            if ((_keyColumnNames.Count > 0)
                && (_keyColumnNames.Count != propertyMappings.Count()))
            {
                throw Error.IncorrectColumnCount(string.Join(", ", _keyColumnNames));
            }

            _keyColumnNames.Each((n, i) => propertyMappings[i].Column.Name = n);

            foreach (var annotation in _annotations)
            {
                var index = _keyColumnNames.IndexOf(annotation.Key.Item1);

                if (index == -1)
                {
                    throw new InvalidOperationException(Strings.BadKeyNameForAnnotation(annotation.Key.Item1, annotation.Key.Item2));
                }

                propertyMappings[index].Column.AddAnnotation(
                    XmlConstants.CustomAnnotationPrefix + annotation.Key.Item2,
                    annotation.Value);
            }
        }
        public void GetEntitySet_should_return_entity_set()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = model.AddEntityType("Foo");
            model.AddEntitySet("FooSet", entityType);

            var entitySet = model.GetEntitySet(entityType);

            Assert.NotNull(entitySet);
            Assert.Same(entityType, entitySet.ElementType);
        }
        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;
        }
        public void MapEntityType_should_create_entity_set_and_add_to_model()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var typeMapper = new TypeMapper(new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), model));
            var mockType = new MockType("Foo");

            var entityType = typeMapper.MapEntityType(mockType);

            var entitySet = model.GetEntitySet(entityType);

            Assert.NotNull(entitySet);
            Assert.Same(entityType, entitySet.ElementType);
            Assert.Equal("Foo", entitySet.Name);
        }
Beispiel #9
0
        private static void ConfigureTable(
            EdmModel database, EntityType table)
        {
            DebugCheck.NotNull(database);
            DebugCheck.NotNull(table);

            var tableName = table.GetTableName();

            if (tableName == null)
            {
                return;
            }

            var entitySet = database.GetEntitySet(table);

            if (!string.IsNullOrWhiteSpace(tableName.Schema))
            {
                entitySet.Schema = tableName.Schema;
            }

            entitySet.Table = tableName.Name;
        }
        private void ConfigureEntitySetName(EntityType entityType, EdmModel model)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(model);

            if ((EntitySetName == null)
                || (entityType.BaseType != null))
            {
                return;
            }

            var entitySet = model.GetEntitySet(entityType);

            Debug.Assert(entitySet != null);

            entitySet.Name
                = model.GetEntitySets().Except(new[] { entitySet }).UniquifyName(EntitySetName);

            entitySet.SetConfiguration(this);
        }