private static void FixNavigationProperties(
     EdmModel model, AssociationType unifiedAssociation, AssociationType redundantAssociation)
 {
     foreach (var navigationProperty
         in model.GetEntityTypes()
                 .SelectMany(e => e.NavigationProperties)
                 .Where(np => np.Association == redundantAssociation))
     {
         navigationProperty.RelationshipType = unifiedAssociation;
         navigationProperty.ToEndMember = unifiedAssociation.SourceEnd;
     }
 }
Example #2
0
        private static void ConfigureTables(EdmModel database)
        {
            DebugCheck.NotNull(database);

            foreach (var table in database.GetEntityTypes().ToList())
            {
                ConfigureTable(database, table);
            }
        }
        internal override void Configure(
            StorageAssociationSetMapping associationSetMapping, EdmModel database, PropertyInfo navigationProperty)
        {
            DebugCheck.NotNull(associationSetMapping);
            DebugCheck.NotNull(database);
            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.GetEntityTypes()
                        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.ColumnProperty)));

                    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].ColumnProperty.Name = n);
        }