示例#1
0
        public static TableDef GetTableDef(Type EntityType)
        {
            EntityType = BaseFactory.GetObjType(EntityType);

            TableDef newTd;

            if (TableDefs.TryGetValue(EntityType, out newTd))
            {
                return(newTd);
            }

            try
            {
                newTd = BuildEntityMetaData(EntityType);
                if (TableDefs.ContainsKey(EntityType))
                {
                    return(newTd);
                }
                else
                {
                    TableDefs.Add(EntityType, newTd);
                    if (!NameTableDefs.ContainsKey(newTd._TableName))
                    {
                        NameTableDefs.Add(newTd._TableName, newTd);
                    }

                    CheckTableDef(newTd);

                    foreach (RelationAttribute ra in (RelationAttribute[])
                             EntityType.GetCustomAttributes(typeof(RelationAttribute), true))
                    {
                        if (ra.ChildType == null)
                        {
                            ra.ChildType = EntityType;
                        }
                        newTd.ChildRelations.Add(ra);

                        TableDef tdParent = MetaData.GetTableDef(ra._ParentType);
                        tdParent.ParentRelations.Add(ra);

                        if (ra._ParentFields == null)
                        {
                            string[] Fields = new string[tdParent.KeyFields.Count];
                            int      i      = 0;
                            foreach (FieldDef fld in tdParent.KeyFields.Values)
                            {
                                Fields[i++] = fld._FieldName;
                            }
                            ra._ParentFields = Fields;
                            ra._ChildFields  = Fields;
                            ra._ParentFields = Fields;
                            ra._ChildFields  = Fields;
                        }

                        CheckChildRelation(ra, tdParent, newTd);
                    }

                    return(newTd);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format(
                                                   ErrorMetaData.BuildMetaData, EntityType.Name,
                                                   ex.Message));
            }
        }