private void RegistForeignKeyMonitor(IEntityProxyInfo childEntityProxy) { if (foreignKeyMap == null) { foreignKeyMap = new Dictionary <Guid, object>(); } var rowId = childEntityProxy.Proxy.Row.InternalId; if (foreignKeyMap.ContainsKey(rowId)) { return; } childEntityProxy.Proxy.Row.PropertyChanged += new PropertyChangedEventHandler(OnNotifyForeignPropertyChanged); string key = childEntityProxy.Proxy.Row.PrimaryKey; //EntityTableHelper<TEntity>.RetriveKey(childEntity); if (string.IsNullOrEmpty(key)) ////PID不可為空 { key = Guid.NewGuid().ToString(); childEntityProxy.Proxy.Row.SetPrimaryKey(key, true); } foreignKeyMap[rowId] = key; this.EntityRelation.Keys.Add(key); UpdateContent(); }
protected void DoAdd(TEntity childEntity) { IEntityProxyInfo childEntityProxy = childEntity as IEntityProxyInfo; if (!string.IsNullOrEmpty(childEntityProxy.Proxy.Row.Table.Namespace)) { SetForeignRelation(childEntityProxy.Proxy.Row.Table.Namespace, childEntityProxy.Proxy.Row.Table.TableName); } //修正所有子ForeignField的孫設定 if (IsEnablePropagateForeignSetting) { PropagateForeignSetting( childEntity , (PropagateForeignSettingMethod != null) ? PropagateForeignSettingMethod : (itemForeignField) => itemForeignField.SetForeignRelation(EntityRelation.Namespace) ); } RegistForeignKeyMonitor(childEntityProxy); }
static public void PropagateForeignSetting(TEntity itemEntity, Action <IForeignSetBaseField> settingMethod) { IEntityProxyInfo childEntityProxy = itemEntity as IEntityProxyInfo; Type type = typeof(TEntity); foreach (var propInfo in type.GetProperties()) { if (typeof(IForeignSetBaseField).IsAssignableFrom(propInfo.PropertyType)) { var itemForeignField = propInfo.GetValue(itemEntity, null) as IForeignSetBaseField; if (itemForeignField == null) { continue; } itemForeignField.PropagateForeignSettingMethod = settingMethod; settingMethod(itemForeignField); } } }