예제 #1
0
        public void Apply_should_ignored_configured_tables()
        {
            var database = new EdmModel(DataSpace.CSpace);
            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);
            table.SetTableName(new DatabaseName("Foo"));
            var entitySet = database.AddEntitySet("ES", table);
            entitySet.Table = "Customer";

            (new PluralizingTableNameConvention()).Apply(table, new DbModel(null, database));

            Assert.Equal("Customer", entitySet.Table);
            Assert.Equal("Foo", table.GetTableName().Name);
        }
예제 #2
0
        public static System.Data.Entity.Core.Metadata.Edm.EntityType FindTableByName(
            this EdmModel database,
            DatabaseName tableName)
        {
            IList <System.Data.Entity.Core.Metadata.Edm.EntityType> entityTypeList = database.EntityTypes as IList <System.Data.Entity.Core.Metadata.Edm.EntityType> ?? (IList <System.Data.Entity.Core.Metadata.Edm.EntityType>)database.EntityTypes.ToList <System.Data.Entity.Core.Metadata.Edm.EntityType>();

            for (int index = 0; index < entityTypeList.Count; ++index)
            {
                System.Data.Entity.Core.Metadata.Edm.EntityType table = entityTypeList[index];
                DatabaseName tableName1 = table.GetTableName();
                if ((tableName1 != null ? (tableName1.Equals(tableName) ? 1 : 0) : (!string.Equals(table.Name, tableName.Name, StringComparison.Ordinal) ? 0 : (tableName.Schema == null ? 1 : 0))) != 0)
                {
                    return(table);
                }
            }
            return((System.Data.Entity.Core.Metadata.Edm.EntityType)null);
        }
        private static bool UpdateColumnNamesForTableSharing(
            DbDatabaseMapping databaseMapping, EntityType entityType, EntityType toTable,
            StorageMappingFragment fragment)
        {
            // Validate: this table can be used only if:
            //  1. The table is not used by any other type
            //  2. The table is used only by types in the same type hierarchy (TPH)
            //  3. There is a 1:1 relationship and the PK count and types match (Table Splitting)
            var typesSharingTable = FindAllTypesUsingTable(databaseMapping, toTable);
            var associationsToSharedTable = new Dictionary<EntityType, List<AssociationType>>();

            foreach (var candidateType in typesSharingTable)
            {
                var oneToOneAssocations = FindAllOneToOneFKAssociationTypes(
                    databaseMapping.Model, entityType, candidateType);

                var rootType = candidateType.GetRootType();
                if (!associationsToSharedTable.ContainsKey(rootType))
                {
                    associationsToSharedTable.Add(rootType, oneToOneAssocations.ToList());
                }
                else
                {
                    associationsToSharedTable[rootType].AddRange(oneToOneAssocations);
                }
            }
            foreach (var candidateTypePair in associationsToSharedTable)
            {
                // Check if these types are in a TPH hierarchy
                if (candidateTypePair.Key != entityType.GetRootType()
                    && candidateTypePair.Value.Count == 0)
                {
                    var tableName = toTable.GetTableName();

                    throw Error.EntityMappingConfiguration_InvalidTableSharing(
                        entityType.Name, candidateTypePair.Key.Name,
                        tableName != null ? tableName.Name : databaseMapping.Database.GetEntitySet(toTable).Table);
                }
            }

            var allAssociations = associationsToSharedTable.Values.SelectMany(l => l);
            if (allAssociations.Any())
            {
                var principalKeyNamesType = toTable.GetKeyNamesType();
                if (principalKeyNamesType == null)
                {
                    // grab a candidate
                    var association = allAssociations.First();
                    principalKeyNamesType = association.Constraint.FromRole.GetEntityType();

                    if (allAssociations.All(x => x.Constraint.FromRole.GetEntityType() == principalKeyNamesType))
                    {
                        toTable.SetKeyNamesType(principalKeyNamesType);
                    }
                }

                // rename the columns in the fragment to match the principal keys
                var principalKeys = principalKeyNamesType.KeyProperties().ToArray();
                var i = 0;
                foreach (var k in entityType.KeyProperties())
                {
                    var dependentColumn = fragment.ColumnMappings.Single(pm => pm.PropertyPath.First() == k).ColumnProperty;
                    dependentColumn.Name = principalKeys[i].Name;
                    i++;
                }
                return true;
            }
            return false;
        }
예제 #4
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;
        }