public async Task <bool> TryToAddProductAsync(Product <IDescription> product) { try { var name = product.Description.GetType().Name + 's'; // Get DbSet<"TableName"> property var property = ReflectionHelper.GetProperty(this, name); // Get property value var propertyValue = property.GetValue(this); // Get property type var type = property.PropertyType; var productType = product.Description.GetType(); product.ProductInfo.AddDate = DateTime.Now; product.ProductInfo.TableName = name; await Products.AddAsync(product.ProductInfo); await SaveChangesAsync(); var id = Products.Last().Id; product.Description.Id = id; // Find and invoke method "Add" in property type type.GetMethod("Add", new Type[1] { productType }) .Invoke(propertyValue, new object[1] { product.Description }); await SaveChangesAsync(); } catch { return(false); } return(true); }
public async Task AddProductAsync(Product product) => await Products.AddAsync(product);