private void CreatePrimaryKeys(StringBuilder script, ISourceProvider provider)
        {
            var pks  = Model.GetActiveEntities().SelectMany(item => item.GetPkProperties());
            var rels = Model.GetActiveRelations().Where(item => item.Constraint != RelationConstraint.None);

            if (pks.Count() == 0 && rels.Count() == 0)
            {
                return;
            }

            bool hdr = false;

            foreach (SourceFragmentDefinition s in pks.Select(item => item.SourceFragment).Distinct())
            {
                SourceFragmentDefinition sf = s;

                var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item =>
                                                                               item.Name == sf.Name && item.Selector == sf.Selector);

                var tablePKs = pks.Where(item => item.SourceFragment == sf);
                //string constraintName = null;
                const bool isPK = true;

                if (targetSF != null)
                {
                    SourceConstraint pk = targetSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName);
                    //if (pk == null)
                    //{
                    //    isPK = false;
                    //    pk = targetSF.Constraints.SingleOrDefault(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName);
                    //}

                    if (pk != null)
                    {
                        if (tablePKs.All(item => pk.SourceFields.Any(pkf => pkf.SourceFieldExpression == item.SourceFieldExpression)))
                        {
                            continue;
                        }
                    }
                    //else
                    //    isPK = true;
                }

                //if (string.IsNullOrEmpty(constraintName))
                //{
                //    constraintName = "PK_" + sf.Name.Trim(']', '[');
                //}
                if (!hdr)
                {
                    script.AppendLine("--Creating primary keys");
                    hdr = true;
                }

                provider.GenerateCreatePKScript(tablePKs.Select(item => new PropDefinition {
                    Field = item.SourceField, Attr = item.Attributes, PropType = item.PropertyType
                }),
                                                "PK_" + sf.Name.Trim(']', '['), script, isPK, true);
            }

            foreach (RelationDefinitionBase rel in rels)
            {
                var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item =>
                                                                               item.Name == rel.SourceFragment.Name && item.Selector == rel.SourceFragment.Selector);

                bool isPK = rel.Constraint == RelationConstraint.PrimaryKey;

                if (targetSF != null)
                {
                    if (isPK)
                    {
                        SourceConstraint pk = targetSF.Constraints.SingleOrDefault(item =>
                                                                                   item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName);
                        if (pk != null)
                        {
                            if (rel.Left.FieldName.Union(rel.Right.FieldName).All(item => pk.SourceFields.Any(pkf => pkf.SourceFieldExpression == item)))
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        if (targetSF.Constraints.Any(item =>
                                                     item.ConstraintType == SourceConstraint.UniqueConstraintTypeName &&
                                                     rel.Left.FieldName.Union(rel.Right.FieldName).All(fld => item.SourceFields.Any(pkf => pkf.SourceFieldExpression == fld))
                                                     ))
                        {
                            continue;
                        }
                    }
                }

                if (!hdr)
                {
                    script.AppendLine("--Creating primary keys");
                    hdr = true;
                }

                provider.GenerateCreatePKScript(rel.Left.FieldName.Union(rel.Right.FieldName)
                                                .Select(item => new PropDefinition {
                    Field = new SourceFieldDefinition(rel.SourceFragment, item)
                })
                                                , isPK?"PK_":"UQ_" + rel.SourceFragment.Name.Trim(']', '['), script, isPK, true);
            }
        }
        private void CreateUniqueConstraints(StringBuilder script, ISourceProvider provider)
        {
            var uks = Model.GetActiveEntities().SelectMany(e => e.GetActiveProperties())
                      .Where(item => item.SourceFragment != null &&
                             item.SourceFragment.Constraints.Any(cns =>
                                                                 cns.ConstraintType == SourceConstraint.UniqueConstraintTypeName));

            if (uks.Count() == 0)
            {
                return;
            }
            bool hdr = false;

            foreach (SourceFragmentDefinition s in uks.Select(item => item.SourceFragment).Distinct())
            {
                SourceFragmentDefinition sf = s;

                var targetSF = SourceView.GetSourceFragments().SingleOrDefault(item =>
                                                                               item.Name == sf.Name && item.Selector == sf.Selector);

                const bool    isPK  = false;
                List <string> names = new List <string>();
                foreach (SourceConstraint constraint in s.Constraints.Where(item =>
                                                                            item.ConstraintType == SourceConstraint.UniqueConstraintTypeName))
                {
                    if (targetSF == null || !targetSF.Constraints.Any(item =>
                                                                      constraint.ConstraintType == item.ConstraintType &&
                                                                      constraint.SourceFields.All(fld =>
                                                                                                  item.SourceFields.Any(sfld => sfld.SourceFieldExpression == fld.SourceFieldExpression))))
                    {
                        var tablePKs = uks.Where(item => item.SourceFragment == sf);
                        List <PropDefinition> tableProps = new List <PropDefinition>();
                        foreach (PropertyDefinition prop in tablePKs)
                        {
                            if (prop is ScalarPropertyDefinition)
                            {
                                ScalarPropertyDefinition p = prop as ScalarPropertyDefinition;
                                if (constraint.SourceFields.Any(item => item.SourceFieldExpression == p.SourceFieldExpression))
                                {
                                    tableProps.Add(new PropDefinition {
                                        Attr = prop.Attributes, Field = p.SourceField, PropType = prop.PropertyType
                                    });
                                }
                            }
                            else
                            {
                                EntityPropertyDefinition p = prop as EntityPropertyDefinition;
                                foreach (EntityPropertyDefinition.SourceField field in p.SourceFields)
                                {
                                    if (constraint.SourceFields.Any(item => item.SourceFieldExpression == field.SourceFieldExpression))
                                    {
                                        tableProps.Add(new PropDefinition {
                                            Attr     = Field2DbRelations.None, Field = field,
                                            PropType = prop.PropertyType.Entity.GetProperties().Single(pr => pr.PropertyAlias == field.PropertyAlias).PropertyType
                                        }
                                                       );
                                    }
                                }
                            }
                        }
                        string constraintName = "UK_" + sf.Name.Trim(']', '[');
                        if (names.Contains(constraintName))
                        {
                            constraintName += "_" + names.Count(item => item.StartsWith(constraintName));
                        }

                        if (!hdr)
                        {
                            script.AppendLine("--Creating unique constraints");
                            hdr = true;
                        }

                        provider.GenerateCreatePKScript(tableProps, constraintName, script, isPK,
                                                        tablePKs.First().Entity.GetPkProperties().Count() == 0);

                        names.Add(constraintName);
                    }
                }
            }
        }