public async Task Remove(T item) { dbContext.Set <T>().Remove(item); await dbContext.SaveChangesAsync(); await OnDbChanged(); }
public async Task <bool> LoadAsync <T>() where T : class { Thread thread = new Thread(() => { try { _dbContext.Set <T>().Load(); _loaded = true; } catch (Exception ex) { Message = ex.Message; MessageDetail = ex?.InnerException?.Message; _error = true; } }); thread.Start(); await Task.Run(() => { while (!_loaded && !_error) { Task.Delay(100); } }); return(!_error); }
public async void Handle(string message) { bool hasId = message.Contains("/"); object id = null; string modelTypeName = hasId ? message.Split('/')[0] : message; string modelName1 = modelTypeName.Substring(modelTypeName.IndexOf('.') + 1); var ass = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.StartsWith("DAL")); Type type = ass.GetType(modelTypeName); var set = allDbContext.Set(type); if (hasId) { id = int.Parse(message.Split('/')[1]); var entity = set.Find(id); await allDbContext.Entry(entity).ReloadAsync(); } //else //{ // await set.ForEachAsync(x => allDbContext.Entry(x).Reload()); //} var eventType = typeof(DataUpdated <>).MakeGenericType(type); var instance = Activator.CreateInstance(eventType) as IEvent; await eventBus.Publish(instance); }
private async Task<FrameworkElement> GetControlFrom(BindingComponent bind, Type modelType) { PropertyInfo info = modelType.GetProperty(bind.PropertyName); Control control = new TextBox(); TextBlock label = new TextBlock() { Text = bind.DisplayName }; control.SetBinding(TextBox.TextProperty, info.Name); if (info.PropertyType == typeof(DateTime)) { control = new DatePicker() { DisplayDateStart = new DateTime(1960, 1, 1)}; if (!IsEdit) { info.SetValue(Item, DateTime.Now); } control.SetBinding(DatePicker.SelectedDateProperty, info.Name); } if (info.PropertyType.IsEnum) { var list = Enum.GetNames(info.PropertyType); control = new ComboBox(); if (list.Length > 0) { (control as ComboBox).ItemsSource = list; (control as ComboBox).SetBinding(ComboBox.SelectedItemProperty, bind.PropertyName); if (!IsEdit) { (control as ComboBox).SelectedIndex = 0; info.SetValue(Item, Enum.Parse(info.PropertyType, (control as ComboBox).SelectedItem.ToString())); } else { var value = info.GetValue(Item); (control as ComboBox).SelectedItem = Enum.GetName(info.PropertyType, value); } } } if(bind.PropertyType == PropertyType.OuterPropertyId || bind.PropertyType == PropertyType.OuterPropertyIdNonVisible) { control = new ComboBox(); string outerPropName = bind.PropertyName.TrimEnd('I', 'd'); var outerProp = modelType.GetProperty(outerPropName); await allDbContext.Set(outerProp.PropertyType).LoadAsync(); var list = await allDbContext.Set(outerProp.PropertyType).ToListAsync(); if (list.Count > 0) { (control as ComboBox).ItemsSource = list; (control as ComboBox).SelectedValuePath = "Id"; (control as ComboBox).DisplayMemberPath = bind.DisplayMember; (control as ComboBox).SetBinding(ComboBox.SelectedValueProperty, bind.PropertyName); if (!IsEdit) { (control as ComboBox).SelectedIndex = 0; info.SetValue(Item, (control as ComboBox).SelectedValue); } } } control.Margin = new Thickness(0, 5, 0, 0); label.HorizontalAlignment = HorizontalAlignment.Left; StackPanel stack = new StackPanel(); stack.Children.Add(label); stack.Children.Add(control); stack.Margin = new Thickness(0, 15, 0, 0); return stack; }