I did not specify the DbSet in my applicationdbcontext.
However, I am able to create order payments using the following method:
public List<OrderPaymentDto> Create(CreateOrderPaymentDto createInput)
{
if (createInput == null) return null;
var orderTotalPrice = this.orderRepository.GetSingleAsync(o => o.Id == createInput.OrderId).Await()?.Price;
if (orderTotalPrice == null)
{
throw new NotFoundException($"An order with an id {createInput.OrderId} has not been found! ");
}
var list = new List<OrderPaymentDto>();
if (createInput.OrderPaymentsTemplateGroupId != null && createInput.OrderPaymentsTemplateGroupId != 0)
{
var orderTemplates = this.orderPaymentsTemplateManager.GetAll(op => op.OrderPaymentsTemplateGroupId == createInput.OrderPaymentsTemplateGroupId);
if (orderTemplates == null)
{
throw new NotFoundException("No order templates were found!");
}
//take the order repository total price
foreach (var orderTemplate in orderTemplates)
{
OrderPayment orderPaymentToBeCreated = new OrderPayment
{
Amount = ((orderTotalPrice.Value * orderTemplate.Amount) / 100),
OrderId = createInput.OrderId,
DueDate = DateTime.Now.AddDays(orderTemplate.PaymentPeriod),
PaymentType = orderTemplate.PaymentType,
Name = orderTemplate.Name
};
var addedOrderPayment = this.repository.AddAsync(orderPaymentToBeCreated).Await();
list.Add(mapper.Map<OrderPaymentDto>(addedOrderPayment));
}
}
else
{
OrderPayment orderPaymentToBeCreated = new OrderPayment
{
Amount = createInput.Amount,
OrderId = createInput.OrderId,
DueDate = DateTime.Now.AddDays(createInput.PaymentPeriod),
PaymentType = createInput.PaymentType,
Name = createInput.Name
};
var addedOrderPayment = this.repository.AddAsync(orderPaymentToBeCreated).Await();
list.Add(mapper.Map<OrderPaymentDto>(addedOrderPayment));
}
this.notificationService.OnCreateEntity("OrderPayment", list);
return list;
}
the repository addasync method is this:
public async Task<TEntity> AddAsync(TEntity entity)
{
ObjectCheck.EntityCheck(entity);
await dbContext.Set<TEntity>().AddAsync(entity);
await dbContext.SaveChangesAsync();
return entity;
}
The table itself is created in PostGre, I am able to create entities.
What is the point of including them in the ApplicationDbContext?
The model itself has a reference to Order which has a dbset in the ApplicationDbContext. If entities are related can I just include one db set and not the rest?
My previous understanding of a DBSet is that it is used to have crud operations on the database. Now my understanding is challenged.
Can someone please clarify?