public int Save(TableDrivedEntityDTO baseEntity, TableDrivedEntityDTO drivedEntity, bool inheritance, bool isTolatParticipation, bool isDisjoint)
        {
            using (var projectContext = new MyIdeaEntities())
            {
                foreach (var item in drivedEntity.Relationships)
                {
                    var          dbRelationship        = projectContext.Relationship.First(x => x.ID == item.ID);
                    Relationship dbReverseRelationship = null;
                    if (dbRelationship.Relationship2 != null)
                    {
                        dbReverseRelationship = dbRelationship.Relationship2;
                    }
                    else
                    {
                        dbReverseRelationship = projectContext.Relationship.First(x => x.RelationshipID == item.ID);
                    }

                    if (dbRelationship.TableDrivedEntity.TableID == baseEntity.TableID)
                    {
                        if (dbRelationship.Relationship2 == null)
                        {
                            dbRelationship.Name = dbRelationship.Name.Replace("(PK)" + dbRelationship.TableDrivedEntity.Name + ".", "(PK)" + drivedEntity.Name + ".");
                        }
                        else
                        {
                            dbRelationship.Name = dbRelationship.Name.Replace("(FK)" + dbRelationship.TableDrivedEntity.Name + ".", "(FK)" + drivedEntity.Name + ".");
                        }

                        dbRelationship.TableDrivedEntityID1 = drivedEntity.ID;
                    }
                    if (dbRelationship.TableDrivedEntity1.TableID == baseEntity.TableID)
                    {
                        if (dbRelationship.Relationship2 == null)
                        {
                            dbRelationship.Name = dbRelationship.Name.Replace("(FK)" + dbRelationship.TableDrivedEntity.Name + ".", "(FK)" + drivedEntity.Name + ".");
                        }
                        else
                        {
                            dbRelationship.Name = dbRelationship.Name.Replace("(PK)" + dbRelationship.TableDrivedEntity.Name + ".", "(PK)" + drivedEntity.Name + ".");
                        }
                        dbRelationship.TableDrivedEntityID2 = drivedEntity.ID;
                    }

                    if (dbReverseRelationship.TableDrivedEntity.TableID == baseEntity.TableID)
                    {
                        if (dbReverseRelationship.Relationship2 == null)
                        {
                            dbReverseRelationship.Name = dbReverseRelationship.Name.Replace("(PK)" + dbRelationship.TableDrivedEntity.Name + ".", "(PK)" + drivedEntity.Name + ".");
                        }
                        else
                        {
                            dbReverseRelationship.Name = dbReverseRelationship.Name.Replace("(FK)" + dbRelationship.TableDrivedEntity.Name + ".", "(FK)" + drivedEntity.Name + ".");
                        }
                        dbReverseRelationship.TableDrivedEntityID1 = drivedEntity.ID;
                    }
                    if (dbReverseRelationship.TableDrivedEntity1.TableID == baseEntity.TableID)
                    {
                        if (dbReverseRelationship.Relationship2 == null)
                        {
                            dbReverseRelationship.Name = dbReverseRelationship.Name.Replace("(FK)" + dbRelationship.TableDrivedEntity.Name + ".", "(FK)" + drivedEntity.Name + ".");
                        }
                        else
                        {
                            dbReverseRelationship.Name = dbReverseRelationship.Name.Replace("(PK)" + dbRelationship.TableDrivedEntity.Name + ".", "(PK)" + drivedEntity.Name + ".");
                        }
                        dbReverseRelationship.TableDrivedEntityID2 = drivedEntity.ID;
                    }
                }
                if (inheritance && !(drivedEntity.ID == baseEntity.ID))
                {
                    ISARelationship isaRelationship = null;

                    var sampleSuperToSub = projectContext.Relationship.FirstOrDefault(x => x.TableDrivedEntityID1 == baseEntity.ID && x.RelationshipType != null &&
                                                                                      x.RelationshipType.SuperToSubRelationshipType != null);
                    if (sampleSuperToSub != null)
                    {
                        isaRelationship = sampleSuperToSub.RelationshipType.SuperToSubRelationshipType.ISARelationship;
                    }

                    if (isaRelationship == null)
                    {
                        isaRelationship = new ISARelationship();
                    }
                    isaRelationship.IsTolatParticipation = isTolatParticipation;
                    isaRelationship.IsDisjoint           = isDisjoint;

                    string subTypesStr = "";
                    foreach (var relationship in isaRelationship.SuperToSubRelationshipType)
                    {
                        if (relationship.RelationshipType.Relationship.TableDrivedEntity1.ID != baseEntity.ID)
                        {
                            subTypesStr += (subTypesStr == "" ? "" : ",") + relationship.RelationshipType.Relationship.TableDrivedEntity1.Name;
                        }
                    }
                    if (drivedEntity.ID == 0)
                    {
                        subTypesStr += (subTypesStr == "" ? "" : ",") + drivedEntity.Name;
                    }
                    isaRelationship.Name = baseEntity.Name + ">" + subTypesStr;

                    if (drivedEntity.ID == 0)
                    {
                        var relationship = new Relationship();
                        relationship.RelationshipType = new RelationshipType();
                        relationship.RelationshipType.SuperToSubRelationshipType = new SuperToSubRelationshipType();
                        relationship.RelationshipType.IsOtherSideCreatable       = true;
                        relationship.TableDrivedEntityID1 = baseEntity.ID;
                        relationship.RelationshipType.SuperToSubRelationshipType.ISARelationship = isaRelationship;
                        //drivedEntity.Relationship1.Add(relationship);

                        var relationshipReverse = new Relationship();
                        relationshipReverse.RelationshipType = new RelationshipType();
                        relationshipReverse.RelationshipType.SubToSuperRelationshipType = new SubToSuperRelationshipType();
                        relationshipReverse.RelationshipType.IsOtherSideCreatable       = true;
                        relationshipReverse.TableDrivedEntityID2 = baseEntity.ID;
                        relationshipReverse.RelationshipType.SubToSuperRelationshipType.ISARelationship = isaRelationship;
                        relationshipReverse.Relationship2 = relationship;
                        //drivedEntity.Relationship.Add(relationshipReverse);

                        var    dbBaseEntity = projectContext.TableDrivedEntity.First(x => x.ID == baseEntity.ID);
                        string PKColumns    = "";
                        string FKColumns    = "";
                        foreach (var primaryCol in dbBaseEntity.Table.Column.Where(x => x.PrimaryKey == true))
                        {
                            PKColumns += (PKColumns == "" ? "" : ",") + primaryCol.Name;
                            FKColumns += (FKColumns == "" ? "" : ",") + primaryCol.Name;
                            relationship.RelationshipColumns.Add(new RelationshipColumns()
                            {
                                Column = primaryCol, Column1 = primaryCol
                            });
                            relationshipReverse.RelationshipColumns.Add(new RelationshipColumns()
                            {
                                Column = primaryCol, Column1 = primaryCol
                            });
                        }
                        relationship.Name        = "(PK)" + baseEntity.Name + "." + PKColumns + ">(FK)" + drivedEntity.Name + "." + FKColumns;
                        relationshipReverse.Name = "(FK)" + drivedEntity.Name + "." + FKColumns + ">(PK)" + baseEntity.Name + "." + PKColumns;
                    }
                }
                //if (drivedEntity.ID == 0)
                //    projectContext.TableDrivedEntity.Add(drivedEntity);
                projectContext.SaveChanges();
                return(0);
            }
        }
        private TableDrivedEntityDTO ToTableDrivedEntityDTO(TableDrivedEntity item, EntityColumnInfoType columnInfoType, EntityRelationshipInfoType relationshipInfoType)
        {
            //var cachedItem = CacheManager.GetCacheManager().GetCachedItem(CacheItemType.Entity, item.ID.ToString(), columnInfoType.ToString(), relationshipInfoType.ToString());
            //if (cachedItem != null)
            //    return (cachedItem as TableDrivedEntityDTO);
            TableDrivedEntityDTO result = new TableDrivedEntityDTO();

            result.Name      = item.Name;
            result.ID        = item.ID;
            result.TableID   = item.TableID;
            result.TableName = item.Table.Name;
            //    result.Schema = item.Table.DBSchema.Name;
            result.EntityListViewID = item.EntityListViewID ?? 0;
            result.DatabaseID       = item.Table.DBSchema.DatabaseInformationID;
            result.DatabaseName     = item.Table.DBSchema.DatabaseInformation.Name;
            result.RelatedSchemaID  = item.Table.DBSchemaID;
            result.RelatedSchema    = item.Table.DBSchema.Name;
            result.ServerID         = item.Table.DBSchema.DatabaseInformation.DBServerID;
            result.Alias            = item.Alias;
            result.Criteria         = item.Criteria;
            //result.SecurityObjectID = item.SecurityObjectID.Value;

            //if (result.UnionTypeEntities == "")
            //    if (item.Relationship.Any(x => (x.RelationshipType == null && x.Relationship2 != null && x.TableDrivedEntity != x.TableDrivedEntity1 && !x.RelationshipColumns.All(y => y.Column.PrimaryKey == true))
            //        || (x.Relationship2 == null && x.TableDrivedEntity != x.TableDrivedEntity1 && !x.RelationshipColumns.All(y => y.Column1.PrimaryKey == true))))
            //        result.UnionTypeEntities = "Choose UnionType";

            result.BatchDataEntry       = item.BatchDataEntry;
            result.IsAssociative        = item.IsAssociative;
            result.IsDataReference      = item.IsDataReference;
            result.IsStructurReferencee = item.IsStructurReferencee;
            BizColumn bizColumn = new BizColumn();

            if (columnInfoType == EntityColumnInfoType.WithSimpleColumns)
            {
                result.Columns = bizColumn.GetColumns(item, true);
            }
            else if (columnInfoType == EntityColumnInfoType.WithFullColumns)
            {
                result.Columns = bizColumn.GetColumns(item, false);
            }
            if (relationshipInfoType == EntityRelationshipInfoType.WithRelationships)
            {
                BizISARelationship   bizISARelationship   = new BizISARelationship();
                BizUnionRelationship bizUnionRelationship = new Paper_MetadataManagement.BizUnionRelationship();
                BizRelationship      bizRelationship      = new BizRelationship();
                foreach (var relationship in item.Relationship)
                {
                    var relationshipDTO = bizRelationship.ToRelationshipDTO(relationship);
                    result.Relationships.Add(relationshipDTO);
                    if (relationship.RelationshipType != null)
                    {
                        if (relationship.RelationshipType.OneToManyRelationshipType != null)
                        {
                            result.OneToManyRelationships.Add(bizRelationship.ToOneToManyRelationship(relationship.RelationshipType.OneToManyRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.ManyToOneRelationshipType != null)
                        {
                            result.ManyToOneRelationships.Add(bizRelationship.ToManyToOneRelationshipDTO(relationship.RelationshipType.ManyToOneRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.ImplicitOneToOneRelationshipType != null)
                        {
                            result.ImplicitOneToOneRelationships.Add(bizRelationship.ToImplicitOneToOneRelationshipDTO(relationship.RelationshipType.ImplicitOneToOneRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.ExplicitOneToOneRelationshipType != null)
                        {
                            result.ExplicitOneToOneRelationships.Add(bizRelationship.ToExplicitOneToOneRelationshipDTO(relationship.RelationshipType.ExplicitOneToOneRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.SuperToSubRelationshipType != null)
                        {
                            result.SuperToSubRelationships.Add(bizISARelationship.ToSuperToSubRelationshipDTO(relationship.RelationshipType.SuperToSubRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.SubToSuperRelationshipType != null)
                        {
                            result.SubToSuperRelationships.Add(bizISARelationship.ToSubToSuperRelationshipDTO(relationship.RelationshipType.SubToSuperRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.UnionToSubUnionRelationshipType != null)
                        {
                            result.SuperUnionToSubUnionRelationships.Add(bizUnionRelationship.ToSuperUnionToSubUnionRelationshipDTO(relationship.RelationshipType.UnionToSubUnionRelationshipType, relationshipDTO));
                        }
                        else if (relationship.RelationshipType.SubUnionToUnionRelationshipType != null)
                        {
                            result.SubUnionToSuperUnionRelationships.Add(bizUnionRelationship.ToSubUnionToSuperUnionRelationshipDTO(relationship.RelationshipType.SubUnionToUnionRelationshipType, relationshipDTO));
                        }
                    }
                }
            }

            //CacheManager.GetCacheManager().AddCacheItem(result, CacheItemType.Entity, item.ID.ToString(), columnInfoType.ToString(), relationshipInfoType.ToString());
            return(result);
        }