public void InputType(Type type) { IMapper mapper = mapperCreator(type); //创建映射器 mappers.Add(type, mapper); var typeRelations = mapper.GetRelations(); //获取该type涉及的所有关系模型 //添加汇总关系模型集合 foreach (var relation in typeRelations) { if (relationModels.Values.Where(a => a.TbName == relation.TbName).ToList().Count == 0) { //如果关系模型字典中没有该关系,则直接添加 relationModels.Add(relation.TbName, relation); } else { //若字典中已经存在该关系,则汇总该关系中的所有列 RelationModel oldRelation = relationModels[relation.TbName]; foreach (var column in relation.Columns) { if (oldRelation.Columns.Where(a => a.ColumnName == column.ColumnName) != null) { //若不存在该列,就添加汇总 oldRelation.Columns.Add(column); } } } } }
public RelationModel GetRelationState() { RelationModel relationState = new RelationModel("_relationstate"); relationState.Columns.Add(new RelationModelColumn("id", "int", null, null)); return(relationState); }
public void InputType(Type type) { IMapper mapper = mapperCreator(type); //创建映射器 if (!mappers.Keys.Contains(type)) { mappers.Add(type, mapper); } foreach (var p in mapper.GetColumnMappingInfos()) { if (p.Property_TypeRole != PropertyMappingInfo.PropertyTypeRole.Value) { Type dependType = p.ReferenceModelType; if (!mappers.Keys.Contains(dependType)) { IMapper _mapper = mapperCreator(dependType); mappers.Add(dependType, _mapper); } } } var typeRelations = mapper.GetRelations(); //获取该type涉及的所有关系模型 //添加汇总关系模型集合 foreach (var relation in typeRelations) { if (relationModels.Values.Where(a => a.TbName == relation.TbName).ToList().Count == 0) { //如果关系模型字典中没有该关系,则直接添加 relationModels.Add(relation.TbName, relation); } else { //若字典中已经存在该关系,则汇总该关系中的所有列 RelationModel oldRelation = relationModels[relation.TbName]; foreach (var column in relation.Columns) { if (oldRelation.Columns.Where(a => a.ColumnName == column.ColumnName).Count() == 0) { //若不存在该列,就添加汇总 oldRelation.Columns.Add(column); } } } } }
/// <summary> /// 获取model所关联的所有关系模型 /// </summary> /// <param name="flag">是否加载全部关系模型(此项为避免循环依赖造成的无限递归)</param> /// <returns></returns> public List <RelationModel> GetRelations(bool flag = true) { List <RelationModel> relations = new List <RelationModel>(); RelationModel mainRelation = new RelationModel(ModelType.Name); relations.Add(mainRelation); var propertys = ModelType.GetProperties(); foreach (var p in propertys) { RelationModelColumn r_column = null; List <RelationModel> new_relations = null; ModelUtil otherModel_util = null; string dbType = null; string typeSize = null; string constraints = string.Empty; foreach (var attr in GetPropertyDbAtttibutes(p.Name)) { if (attr is ConstraintAttribute) { constraints += ((ConstraintAttribute)attr).GetConstraintStr() + " "; } } var propertyMappingInfo = GetPropertyMappingInfo(p.Name); switch (propertyMappingInfo.Property_TypeRole) { case PropertyMappingInfo.PropertyTypeRole.Value: dbType = GetPropertyDbAttribute <TypeAttribute>(p.Name).DbTypeName; typeSize = GetPropertyDbAttribute <TypeAttribute>(p.Name).Size; r_column = new RelationModelColumn(p.Name, dbType, typeSize, constraints); if (GetPropertyDbAttribute <IdentityAttribute>(p.Name) != null) { r_column.ReadOnly = true; } break; case PropertyMappingInfo.PropertyTypeRole.Model: otherModel_util = new ModelUtil(propertyMappingInfo.ReferenceModelType); dbType = otherModel_util.GetPropertyDbAttribute <TypeAttribute>(otherModel_util.PrimaryKeyPropertyName).DbTypeName; typeSize = otherModel_util.GetPropertyDbAttribute <TypeAttribute>(otherModel_util.PrimaryKeyPropertyName).Size; r_column = new RelationModelColumn(p.Name, dbType, typeSize, "NOT NULL"); // 设置外键 new_relations = otherModel_util.GetRelations(); break; case PropertyMappingInfo.PropertyTypeRole.ModelList_To_Obj: otherModel_util = new ModelUtil(propertyMappingInfo.ReferenceModelType); new_relations = otherModel_util.GetRelations(); dbType = GetPropertyDbAttribute <TypeAttribute>(PrimaryKeyPropertyName).DbTypeName; typeSize = GetPropertyDbAttribute <TypeAttribute>(PrimaryKeyPropertyName).Size; //被参照关系的主关系添加一列参照属性 new_relations[0].Columns.Add(new RelationModelColumn(PrimaryKeyPropertyName, dbType, typeSize, "NOT NULL")); break; case PropertyMappingInfo.PropertyTypeRole.ModelList_To_List: if (flag == false) { break; } otherModel_util = new ModelUtil(propertyMappingInfo.ReferenceModelType); //新建第三方参照relation new_relations = new List <RelationModel>(); var _newRelation = new RelationModel(string.Format("{0}_{1}", ModelType.Name, otherModel_util.ModelType.Name)); //添加第一参照列 dbType = GetPropertyDbAttribute <TypeAttribute>(PrimaryKeyPropertyName).DbTypeName; typeSize = GetPropertyDbAttribute <TypeAttribute>(PrimaryKeyPropertyName).Size; _newRelation.Columns.Add(new RelationModelColumn(string.Format("{0}_{1}", ModelType.Name, PrimaryKeyPropertyName), dbType, typeSize, "NOT NULL")); //添加第二参照列 dbType = GetPropertyDbAttribute <TypeAttribute>(otherModel_util.PrimaryKeyPropertyName).DbTypeName; typeSize = GetPropertyDbAttribute <TypeAttribute>(otherModel_util.PrimaryKeyPropertyName).Size; _newRelation.Columns.Add(new RelationModelColumn(string.Format("{0}_{1}", otherModel_util.ModelType.Name, otherModel_util.PrimaryKeyPropertyName), dbType, typeSize, "NOT NULL")); //添加第三参照关系 new_relations.Add(_newRelation); new_relations.AddRange(otherModel_util.GetRelations(false)); break; } if (r_column != null) { mainRelation.Columns.Add(r_column); r_column = null; } if (new_relations != null) { relations.AddRange(new_relations); } new_relations = null; // 清空新建关系 } return(relations); }