/// <summary> /// 类似List(int)这样的值成员的集合所对应的表 /// </summary> /// <param name="root"></param> /// <param name="master"></param> /// <param name="memberField"></param> /// <param name="objectType"></param> /// <returns></returns> private static DataTable CreateValueListTable(DataTable root, DataTable master, IDataField memberField, Type objectType) { var valueListField = memberField as ValueListField; string tableName = string.Format("{0}_{1}", master.Name, memberField.Name); IDataField[] fields = null; var reflectedType = valueListField.GetReflectedType(); //就算集合的成员类型各自不同,但是他们肯定继承至同一个根类,因此中间表是统一一个类型的 if (root.IsEqualsOrDerivedOrInherited(master)) { var rootField = GetForeignKey(master, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex); var valueField = GeneratedField.CreatePrimitiveValue(reflectedType, valueListField); fields = new IDataField[] { rootField, indexField, valueField }; } else { var rootField = GetForeignKey(root, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); //中间表中追加根字段,可以有效防止数据重叠 var masterField = GetForeignKey(master, GeneratedFieldType.MasterKey, DbFieldType.NonclusteredIndex); var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex); var valueField = GeneratedField.CreatePrimitiveValue(reflectedType, valueListField); fields = new IDataField[] { rootField, masterField, indexField, valueField }; } var middle = DataTable.CreateMiddle(root, master, tableName, DataTableType.Middle, fields, memberField); return(middle); }
/// <summary> /// /// </summary> /// <param name="master"></param> /// <param name="slave"></param> /// <param name="memberField"></param> /// <returns></returns> private static DataTable CreateMiddleTable(DataTable slave, IDataField memberField) { var root = slave.Root; var master = slave.Master; string tableName = string.Format("{0}_{1}", master.Name, memberField.Name); IDataField[] fields = null; var objectType = (memberField as ObjectField).GetReflectedType(); //就算集合的成员类型各自不同,但是他们肯定继承至同一个根类,因此中间表是统一一个类型的 if (root.IsEqualsOrDerivedOrInherited(master)) { var rootField = GetForeignKey(master, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); var slaveField = GetForeignKey(slave, GeneratedFieldType.SlaveKey, DbFieldType.NonclusteredIndex); slaveField.ParentMemberField = memberField; var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex); //注意,大多数查询都是以rootField 为条件, indexField为排序,输出slaveField字段, //所以slaveField的位置在最后 fields = new IDataField[] { rootField, indexField, slaveField }; } else { var rootField = GetForeignKey(root, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); //中间表中追加根字段,可以有效防止数据重叠 var masterField = GetForeignKey(master, GeneratedFieldType.MasterKey, DbFieldType.NonclusteredIndex); var slaveField = GetForeignKey(slave, GeneratedFieldType.SlaveKey, DbFieldType.NonclusteredIndex); slaveField.ParentMemberField = memberField; var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex); //注意,大多数查询都是以rootField, masterField为条件, indexField为排序,输出slaveField字段, //所以slaveField的位置在最后 fields = new IDataField[] { rootField, masterField, indexField, slaveField }; } var middle = DataTable.CreateMiddle(root, master, tableName, DataTableType.Middle, fields, memberField); middle.Slave = slave; //如果从表是根,那么需要记录从表和中间表的联系,当删除根对象时,会删除该中间表的数据 RootIsSlaveIndex.TryAdd(middle); return(middle); }