private Dictionary<EntitySet, Table> ConvertEntitySetsToTables(DatabaseSchema schema, EntityContainer container)
        {
            Dictionary<EntitySet, Table> entitySetToTableMapping = new Dictionary<EntitySet, Table>();

            foreach (EntitySet entitySet in container.EntitySets)
            {
                Table table = new Table(container.Name, entitySet.Name);
                schema.Tables.Add(table);
                entitySetToTableMapping.Add(entitySet, table);
                var type = entitySet.EntityType;
                var pk = new PrimaryKey("PK_" + table.Name);
                Dictionary<MemberProperty, Column> propertyToColumnMap = new Dictionary<MemberProperty, Column>();
                foreach (MemberProperty prop in type.Properties)
                {
                    var column = this.ConvertPropertyToColumn(prop);
                    propertyToColumnMap.Add(prop, column);

                    if (prop.IsPrimaryKey)
                    {
                        pk.Columns.Add(column);
                    }

                    table.Columns.Add(column);
                }

                if (pk.Columns.Count > 0)
                {
                    table.PrimaryKey = pk;
                }

                foreach (EdmUniqueConstraint edmUniqueConstraint in type.EdmUniqueConstraints)
                {
                    var uniqueConstraint = new UniqueConstraint("UC_" + table.Name + "_" + edmUniqueConstraint.Name);
                    foreach (var prop in edmUniqueConstraint.Properties)
                    {
                        Column column;
                        ExceptionUtilities.Assert(propertyToColumnMap.TryGetValue(prop, out column), "Edm Unique Constraint's property '{0}' is not found on entity type '{1}'.", prop.Name, type.FullName);
                        uniqueConstraint.Add(column);
                    }

                    table.Add(uniqueConstraint);
                }
            }

            return entitySetToTableMapping;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Adds new <see cref="PrimaryKey"/> to the <see cref="Table"/>.
 /// </summary>
 /// <param name="constraint">Constraint to add</param>
 /// <remarks>Only one <see cref="PrimaryKey"/> is allowed per table.</remarks>
 public void Add(PrimaryKey constraint)
 {
     this.TableType.Add(constraint);
 }