예제 #1
0
        /// <summary>
        /// Import data structure.
        /// </summary>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <returns>
        /// The <see cref="DatabaseModel"/>.
        /// </returns>
        public DatabaseModel Import(object options)
        {
            Logger.Trace("Started Import()");

            DatabaseModel     result     = new DatabaseModel();
            FileSourceOptions fileOption = options as FileSourceOptions;

            var edmx             = Edmx.Load(fileOption.Path);
            var entityTableNames = new HashSet <string>(
                edmx.Runtime.ConceptualModels.Schema.EntityTypes.Select(tbl => tbl.Name.ToUpper()));

            foreach (var table in edmx.Runtime.ConceptualModels.Schema.EntityTypes)
            {
                var tbl = new Table
                {
                    TableName  = table.Name,
                    SchemaName = new TiraggoEntityInfo(
                        edmx,
                        $"{edmx.Runtime.ConceptualModels.Schema.Namespace}.{table.Name}")
                                 .StorageInfo.Schema
                };

                result.Tables.Add(tbl);

                foreach (var col in table.Properties)
                {
                    var column = new Column
                    {
                        ColumnName     = col.Name,
                        DomainDataType = this.MapDatabaseType(col.Type, null),
                        IsPrimaryKey   = table.Key.Any(pk => pk.Name == col.Name),
                        IsRequired     =
                            (col.NullableSpecified && !col.Nullable) ||
                            table.Key.Any(pk => pk.Name == col.Name),
                        ColumnOrder = table.Properties.ToList().IndexOf(col) + 1,
                        Precision   = (col.Precision > 0 && col.Scale > 0) ? col.Precision : 0,
                        Scale       = (col.Precision > 0 && col.Scale > 0) ? col.Scale : 0,
                        Length      = string.IsNullOrEmpty(col.MaxLength)
                                                      ? 0
                                                      : Convert.ToInt32(col.MaxLength)
                    };

                    if (column.IsPrimaryKey)
                    {
                        tbl.DatabaseGeneratedKeyType = this.MapDatabaseGeneratedKey(col.StoreGeneratedPattern);
                    }

                    tbl.Columns.Add(column);
                }

                // var relationships = edmx.Runtime.StorageModels.Schemas.Associations.Where(ass => (ass.ReferentialConstraint.Dependent.Role == table.Name || ass.ReferentialConstraint.Principal.Role == table.Name) &&
                // (entityTableNames.Contains(ass.ReferentialConstraint.Dependent.Role.ToUpper()) &&
                // entityTableNames.Contains(ass.ReferentialConstraint.Principal.Role.ToUpper())));

                // Todo: Work in progress
                var relationships = edmx.Runtime.StorageModels.Schema.Associations.Where(
                    ass => (ass.ReferentialConstraint.Dependent.Role == table.Name ||
                            ass.ReferentialConstraint.Principal.Role == table.Name) &&
                    entityTableNames.Contains(ass.ReferentialConstraint.Principal.Role.ToUpper()));

                foreach (var rel in relationships)
                {
                    var ass = new Relationship
                    {
                        ReferencedTableName =
                            (rel.ReferentialConstraint.Dependent.Role == table.Name)
                                              ? rel.ReferentialConstraint.Principal.Role
                                              : rel.ReferentialConstraint.Dependent.Role,
                        ColumnName =
                            (rel.ReferentialConstraint.Dependent.Role == table.Name)
                                              ? rel.ReferentialConstraint.Dependent.PropertyRef.Name
                                              : rel.ReferentialConstraint.Principal.PropertyRef.Name,
                        ReferencedColumnName =
                            (rel.ReferentialConstraint.Principal.Role == table.Name)
                                              ? rel.ReferentialConstraint.Dependent.PropertyRef.Name
                                              : rel.ReferentialConstraint.Principal.PropertyRef.Name,
                        DependencyRelationShip =
                            (rel.ReferentialConstraint.Dependent.Role == table.Name)
                                              ? RelationshipType.ForeignKey
                                              : RelationshipType.ForeignKeyChild,
                        RelationshipName = rel.Name,

                        SchemaName = new TiraggoEntityInfo(
                            edmx,
                            $"{edmx.Runtime.ConceptualModels.Schema.Namespace}.{table.Name}")
                                     .StorageInfo.Schema
                    };

                    if (rel.Ends.Count() != 2)
                    {
                        throw new Exception("Error in association multiplicity");
                    }

                    var tblMp    = rel.Ends.First(o => o.Role == table.Name);
                    var refTblMp = rel.Ends.First(o => o.Role == ass.ReferencedTableName);

                    ass.Multiplicity           = this.MapMultiplicity(tblMp.Multiplicity);
                    ass.ReferencedMultiplicity = this.MapMultiplicity(refTblMp.Multiplicity);


                    // check For Recursive Reference:
                    var  checker   = rel.Ends.First().Type;
                    bool isSelfRef = true;

                    foreach (var item in rel.Ends)
                    {
                        if (checker != item.Type)
                        {
                            isSelfRef = false;
                        }
                    }

                    Relationship assClone = null;
                    if (isSelfRef)
                    {
                        ass.RelatedTable        = tbl;
                        ass.ReferencedTableName = table.Name;
                        ass.Table             = tbl;
                        ass.RelationshipAlias = table.Name + "_1";

                        assClone = ass.Clone() as Relationship;
                        assClone.RelationshipAlias      = table.Name + "_2";
                        assClone.ColumnName             = ass.ReferencedColumnName;
                        assClone.ReferencedColumnName   = ass.ColumnName;
                        assClone.DependencyRelationShip = RelationshipType.ForeignKey;
                        assClone.Multiplicity           = ass.ReferencedMultiplicity;
                        assClone.ReferencedMultiplicity = ass.Multiplicity;
                    }


                    tbl.Relationships.Add(ass);
                    if (assClone != null)
                    {
                        tbl.Relationships.Add(assClone);
                    }
                }

                FormatNavigationPropertiesToBeUnique(tbl);
            }



            this.Fix(result);

            Logger.Trace("Completed Import()");
            return(result);
        }