Example #1
0
        internal TableModelCache GetTableModel(Type mType)
        {
            var key = GetModelKey(mType.FullName);

            return(TableCache.GetOrAdd(key, k =>
            {
                var tm = new TableModelCache();
                tm.MType = mType;
                var ta = DC.AH.GetAttribute <XTableAttribute>(mType) as XTableAttribute;
                if (ta == null)
                {
                    throw XConfig.EC.Exception(XConfig.EC._004, $"类 [[{mType.FullName}]] 必须是与 DB Table 对应的实体类,并且要由 [XTable] 标记指定类对应的表名!!!");
                }
                tm.MProps = DC.GH.GetPropertyInfos(mType);
                tm.TbCols = DC.SqlProvider.GetColumnsInfos(ta.Name);
                if (tm.TbCols == null ||
                    tm.TbCols.Count <= 0)
                {
                    throw XConfig.EC.Exception(XConfig.EC._028, $"表 [[{DC.XConn.Conn.Database}.{ta.Name}]] 中不存在任何列!!!");
                }
                tm.TbAttr = new XTableAttribute {
                    Name = tm.TbCols.First().TableName
                };
                var list = new List <TmPropColAttrInfo>();
                foreach (var p in tm.MProps)
                {
                    var pca = new TmPropColAttrInfo();
                    pca.Prop = p;
                    var ca = DC.AH.GetAttribute <XColumnAttribute>(mType, p) as XColumnAttribute;
                    if (ca == null ||
                        ca.Name.IsNullStr())
                    {
                        pca.Col = tm.TbCols.FirstOrDefault(it => it.ColumnName.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
                        if (pca.Col == null)
                        {
                            throw XConfig.EC.Exception(XConfig.EC._034, $"属性 [[{mType.Name}.{p.Name}]] 在表 [[{DC.XConn.Conn.Database}.{tm.TbName}]] 中无对应的列!!!");
                        }
                    }
                    else
                    {
                        pca.Col = tm.TbCols.FirstOrDefault(it => it.ColumnName.Equals(ca.Name, StringComparison.OrdinalIgnoreCase));
                        if (pca.Col == null)
                        {
                            throw XConfig.EC.Exception(XConfig.EC._035, $"属性 [[{mType.Name}.{p.Name}]] 上 [XColumn] 标注的字段名 [[{ca.Name}]] 有误!!!");
                        }
                    }
                    pca.ColAttr = new XColumnAttribute {
                        Name = pca.ColName
                    };
                    pca.TbAttr = tm.TbAttr;
                    list.Add(pca);
                }
                tm.PCA = list;
                return tm;
            }));
        }