Exemplo n.º 1
0
 static IEnumerable <string> GenerateForeignKeys(StoreItemCollection storeItems)
 {
     foreach (var associationSet in storeItems.GetItems <EntityContainer>()[0].BaseEntitySets.OfType <AssociationSet>())
     {
         var result = new StringBuilder();
         ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single <ReferentialConstraint>();
         AssociationSetEnd     end        = associationSet.AssociationSetEnds[constraint.FromRole.Name];
         AssociationSetEnd     end2       = associationSet.AssociationSetEnds[constraint.ToRole.Name];
         result.AppendFormat("ALTER TABLE {0}.{1} ADD FOREIGN KEY ({2}) REFERENCES {3}.{4}({5}){6};",
                             SqlGenerator.QuoteIdentifier(MetadataHelpers.GetSchemaName(end2.EntitySet)),
                             SqlGenerator.QuoteIdentifier(MetadataHelpers.GetTableName(end2.EntitySet)),
                             string.Join(", ", constraint.ToProperties.Select(fk => SqlGenerator.QuoteIdentifier(fk.Name))),
                             SqlGenerator.QuoteIdentifier(MetadataHelpers.GetSchemaName(end.EntitySet)),
                             SqlGenerator.QuoteIdentifier(MetadataHelpers.GetTableName(end.EntitySet)),
                             string.Join(", ", constraint.FromProperties.Select(pk => SqlGenerator.QuoteIdentifier(pk.Name))),
                             end.CorrespondingAssociationEndMember.DeleteBehavior == OperationAction.Cascade ? " ON DELETE CASCADE" : string.Empty);
         yield return(result.ToString());
     }
 }
Exemplo n.º 2
0
 static IEnumerable <string> GenerateTables(StoreItemCollection storeItems)
 {
     foreach (var entitySet in storeItems.GetItems <EntityContainer>()[0].BaseEntitySets.OfType <EntitySet>())
     {
         var result     = new StringBuilder();
         var tableName  = MetadataHelpers.GetTableName(entitySet);
         var schemaName = MetadataHelpers.GetSchemaName(entitySet);
         result.AppendFormat("CREATE TABLE {0}.{1} (", SqlGenerator.QuoteIdentifier(schemaName), SqlGenerator.QuoteIdentifier(tableName));
         result.AppendLine();
         result.Append("\t");
         result.Append(string.Join("," + Environment.NewLine + "\t", MetadataHelpers.GetProperties(entitySet.ElementType).Select(p => GenerateColumn(p))));
         result.Append(");");
         result.AppendLine();
         result.AppendFormat("ALTER TABLE {0}.{1} ADD PRIMARY KEY ({2});",
                             SqlGenerator.QuoteIdentifier(schemaName),
                             SqlGenerator.QuoteIdentifier(tableName),
                             string.Join(", ", entitySet.ElementType.KeyMembers.Select(pk => SqlGenerator.QuoteIdentifier(pk.Name))));
         result.AppendLine();
         yield return(result.ToString());
     }
 }